Following the Player
Follow Player
The FollowPlayer node, which will be positioned as a child of the main Camera Node, manages the camera's position by tracking the Player's movement. It also enforces boundaries, ensuring the camera's view never extends beyond the Tilemap's edges, thus preventing the blank background from being visible to the user.
Notice that the script includes a check to ensure the Player Node is still valid before attempting to access its position. This precaution is necessary because the Player might be destroyed during gameplay (e.g., upon being hit), and accessing an invalid pointer would cause an error.
#pragma once
#include "engine/node.h"
#include "engine/tilemap.h"
#include "player.h"
namespace game {
class FollowPlayer : public ng::Node {
public:
FollowPlayer(ng::App* app, const Player* player, const ng::Tilemap* tilemap);
protected:
void OnAdd() override;
void Update() override;
private:
void Follow();
const Player* player_ = nullptr;
const ng::Tilemap* tilemap_ = nullptr;
};
} // namespace gameThe previous method of directly centering the view from within the player node is now obsolete. You can safely remove that centering logic.
Last updated