Setup game camera and player transformed view following/offset.

main
sigonasr2 3 months ago
parent 2b20570eb3
commit 36eb4ce61e
  1. 20
      src/Hamster.cpp
  2. 8
      src/Hamster.h
  3. 7
      src/HamsterGame.cpp
  4. 4
      src/olcUTIL_Camera2D.h

@ -42,14 +42,16 @@ All rights reserved.
#include <ranges>
std::vector<Hamster>Hamster::HAMSTER_LIST;
const uint8_t Hamster::MAX_HAMSTER_COUNT{100U};
const uint8_t Hamster::NPC_HAMSTER_COUNT{5U};
const std::vector<std::string>Hamster::NPC_HAMSTER_IMAGES{
"hamster.png",
};
const std::string Hamster::PLAYER_HAMSTER_IMAGE{"hamster.png"};
std::optional<Hamster*>Hamster::playerHamster;
Hamster::Hamster(const vf2d spawnPos,const std::string_view img,const PlayerControlled playerControlled)
:pos(spawnPos),playerControlled(playerControlled){
Hamster::Hamster(const vf2d spawnPos,const std::string_view img,const PlayerControlled IsPlayerControlled)
:pos(spawnPos),IsPlayerControlled(IsPlayerControlled){
animations=HamsterGame::GetAnimations(img);
animations.ChangeState(internalAnimState,HamsterGame::DEFAULT);
}
@ -62,7 +64,10 @@ void Hamster::UpdateHamsters(const float fElapsedTime){
void Hamster::LoadHamsters(const vf2d startingLoc){
HAMSTER_LIST.clear();
HAMSTER_LIST.emplace_back(startingLoc,PLAYER_HAMSTER_IMAGE,PLAYER_CONTROLLED);
playerHamster.reset();
HAMSTER_LIST.reserve(MAX_HAMSTER_COUNT);
if(NPC_HAMSTER_COUNT+1>MAX_HAMSTER_COUNT)throw std::runtime_error{std::format("WARNING! Max hamster count is too high! Please expand the MAX_HAMSTER_COUNT if you want more hamsters. Requested {} hamsters.",MAX_HAMSTER_COUNT)};
playerHamster=&HAMSTER_LIST.emplace_back(startingLoc,PLAYER_HAMSTER_IMAGE,PLAYER_CONTROLLED);
for(int i:std::ranges::iota_view(0U,NPC_HAMSTER_COUNT)){
HAMSTER_LIST.emplace_back(startingLoc,NPC_HAMSTER_IMAGES.at(util::random()%NPC_HAMSTER_IMAGES.size()),NPC);
}
@ -77,4 +82,13 @@ void Hamster::DrawHamsters(TransformedView&tv){
const Animate2D::Frame&Hamster::GetCurrentAnimation()const{
return animations.GetFrame(internalAnimState);
}
const Hamster&Hamster::GetPlayer(){
if(!playerHamster.has_value())throw std::runtime_error{std::format("WARNING! Player is not created at this time! There should not be any code referencing the player at this moment!")};
return *playerHamster.value();
}
const vf2d&Hamster::GetPos()const{
return pos;
}

@ -49,6 +49,7 @@ class Hamster{
static std::vector<Hamster>HAMSTER_LIST;
static const uint8_t MAX_HAMSTER_COUNT;
static const uint8_t NPC_HAMSTER_COUNT;
static const std::vector<std::string>NPC_HAMSTER_IMAGES;
@ -60,11 +61,14 @@ class Hamster{
std::string img;
Animate2D::Animation<HamsterGame::AnimationState>animations;
Animate2D::AnimationState internalAnimState;
PlayerControlled playerControlled;
PlayerControlled IsPlayerControlled;
static std::optional<Hamster*>playerHamster;
public:
Hamster(const vf2d spawnPos,const std::string_view img,const PlayerControlled playerControlled=NPC);
Hamster(const vf2d spawnPos,const std::string_view img,const PlayerControlled IsPlayerControlled=NPC);
static const Hamster&GetPlayer();
static void UpdateHamsters(const float fElapsedTime);
static void LoadHamsters(const vf2d startingLoc);
static void DrawHamsters(TransformedView&tv);
const Animate2D::Frame&GetCurrentAnimation()const;
const vf2d&GetPos()const;
};

@ -13,7 +13,9 @@ HamsterGame::HamsterGame()
}
bool HamsterGame::OnUserCreate(){
tv.Initialise({320,288},{1,1});
camera=Camera2D{SCREEN_FRAME.size};
camera.SetMode(Camera2D::Mode::LazyFollow);
tv.Initialise(SCREEN_FRAME.size,{1,1});
tv.SetWorldOffset(-SCREEN_FRAME.pos);
LoadGraphics();
LoadAnimations();
@ -54,9 +56,12 @@ void HamsterGame::LoadAnimations(){
void HamsterGame::LoadLevel(){
const vf2d levelSpawnLoc{50,50}; //TEMPORARY
Hamster::LoadHamsters(levelSpawnLoc);
camera.SetTarget(Hamster::GetPlayer().GetPos());
}
void HamsterGame::UpdateGame(const float fElapsedTime){
camera.Update(fElapsedTime);
tv.SetWorldOffset(-SCREEN_FRAME.pos+camera.GetViewPosition());
Hamster::UpdateHamsters(fElapsedTime);
}

@ -116,7 +116,7 @@ namespace olc::utils
}
// Set tracked point via pointer
inline void SetTarget(olc::vf2d& vTarget)
inline void SetTarget(const olc::vf2d& vTarget)
{
m_pTarget = &vTarget;
}
@ -242,7 +242,7 @@ namespace olc::utils
Mode m_nMode = Mode::Simple;
// Target Vector2D object camera should follow (either ref or ptr)
olc::vf2d* m_pTarget = nullptr;
const olc::vf2d* m_pTarget = nullptr;
olc::vf2d m_vLocalTarget;
// World Boundary

Loading…
Cancel
Save