mirror of
https://github.com/sigonasr2/hamster.git
synced 2025-04-18 06:39:39 -05:00
Force 30 FPS minimum elapsed time on game engine. Added directional turn controls for the player.
This commit is contained in:
parent
36eb4ce61e
commit
cf7121701a
Binary file not shown.
Before Width: | Height: | Size: 1.6 KiB After Width: | Height: | Size: 1.4 KiB |
@ -59,6 +59,10 @@ Hamster::Hamster(const vf2d spawnPos,const std::string_view img,const PlayerCont
|
|||||||
void Hamster::UpdateHamsters(const float fElapsedTime){
|
void Hamster::UpdateHamsters(const float fElapsedTime){
|
||||||
for(Hamster&h:HAMSTER_LIST){
|
for(Hamster&h:HAMSTER_LIST){
|
||||||
h.animations.UpdateState(h.internalAnimState,fElapsedTime);
|
h.animations.UpdateState(h.internalAnimState,fElapsedTime);
|
||||||
|
if(h.IsPlayerControlled){
|
||||||
|
h.HandlePlayerControls();
|
||||||
|
}
|
||||||
|
h.TurnTowardsTargetDirection();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -92,3 +96,26 @@ const Hamster&Hamster::GetPlayer(){
|
|||||||
const vf2d&Hamster::GetPos()const{
|
const vf2d&Hamster::GetPos()const{
|
||||||
return pos;
|
return pos;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void Hamster::HandlePlayerControls(){
|
||||||
|
vf2d aimingDir{};
|
||||||
|
if(HamsterGame::Game().GetKey(W).bHeld){
|
||||||
|
aimingDir+=vf2d{0,-1};
|
||||||
|
}
|
||||||
|
if(HamsterGame::Game().GetKey(D).bHeld){
|
||||||
|
aimingDir+=vf2d{1,0};
|
||||||
|
}
|
||||||
|
if(HamsterGame::Game().GetKey(S).bHeld){
|
||||||
|
aimingDir+=vf2d{0,1};
|
||||||
|
}
|
||||||
|
if(HamsterGame::Game().GetKey(A).bHeld){
|
||||||
|
aimingDir+=vf2d{-1,0};
|
||||||
|
}
|
||||||
|
if(aimingDir!=vf2d{}){
|
||||||
|
targetRot=aimingDir.norm().polar().y;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void Hamster::TurnTowardsTargetDirection(){
|
||||||
|
util::turn_towards_direction(rot,targetRot,turnSpd*HamsterGame::Game().GetElapsedTime());
|
||||||
|
}
|
@ -58,6 +58,8 @@ class Hamster{
|
|||||||
vf2d pos;
|
vf2d pos;
|
||||||
vf2d vel;
|
vf2d vel;
|
||||||
float rot{};
|
float rot{};
|
||||||
|
float targetRot{};
|
||||||
|
float turnSpd{2.f*geom2d::pi};
|
||||||
std::string img;
|
std::string img;
|
||||||
Animate2D::Animation<HamsterGame::AnimationState>animations;
|
Animate2D::Animation<HamsterGame::AnimationState>animations;
|
||||||
Animate2D::AnimationState internalAnimState;
|
Animate2D::AnimationState internalAnimState;
|
||||||
@ -71,4 +73,6 @@ public:
|
|||||||
static void DrawHamsters(TransformedView&tv);
|
static void DrawHamsters(TransformedView&tv);
|
||||||
const Animate2D::Frame&GetCurrentAnimation()const;
|
const Animate2D::Frame&GetCurrentAnimation()const;
|
||||||
const vf2d&GetPos()const;
|
const vf2d&GetPos()const;
|
||||||
|
void HandlePlayerControls();
|
||||||
|
void TurnTowardsTargetDirection();
|
||||||
};
|
};
|
@ -6,10 +6,11 @@ geom2d::rect<float>HamsterGame::SCREEN_FRAME{{96,0},{320,288}};
|
|||||||
std::unordered_map<std::string,Animate2D::Animation<HamsterGame::AnimationState>>HamsterGame::ANIMATIONS;
|
std::unordered_map<std::string,Animate2D::Animation<HamsterGame::AnimationState>>HamsterGame::ANIMATIONS;
|
||||||
std::unordered_map<std::string,Renderable>HamsterGame::GFX;
|
std::unordered_map<std::string,Renderable>HamsterGame::GFX;
|
||||||
const std::string HamsterGame::ASSETS_DIR{"assets/"};
|
const std::string HamsterGame::ASSETS_DIR{"assets/"};
|
||||||
|
PixelGameEngine*HamsterGame::self{nullptr};
|
||||||
|
|
||||||
HamsterGame::HamsterGame()
|
HamsterGame::HamsterGame(){
|
||||||
{
|
|
||||||
sAppName = "Project Hamster";
|
sAppName = "Project Hamster";
|
||||||
|
HamsterGame::self=this;
|
||||||
}
|
}
|
||||||
|
|
||||||
bool HamsterGame::OnUserCreate(){
|
bool HamsterGame::OnUserCreate(){
|
||||||
@ -92,6 +93,10 @@ bool HamsterGame::OnUserDestroy(){
|
|||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
PixelGameEngine&HamsterGame::Game(){
|
||||||
|
return *self;
|
||||||
|
}
|
||||||
|
|
||||||
int main()
|
int main()
|
||||||
{
|
{
|
||||||
HamsterGame game;
|
HamsterGame game;
|
||||||
|
@ -60,6 +60,7 @@ public:
|
|||||||
|
|
||||||
static const Renderable&GetGFX(const std::string_view img);
|
static const Renderable&GetGFX(const std::string_view img);
|
||||||
static const Animate2D::Animation<HamsterGame::AnimationState>&GetAnimations(const std::string_view img);
|
static const Animate2D::Animation<HamsterGame::AnimationState>&GetAnimations(const std::string_view img);
|
||||||
|
static PixelGameEngine&Game();
|
||||||
private:
|
private:
|
||||||
void UpdateGame(const float fElapsedTime);
|
void UpdateGame(const float fElapsedTime);
|
||||||
void DrawGame();
|
void DrawGame();
|
||||||
@ -70,4 +71,5 @@ private:
|
|||||||
void _LoadImage(const std::string_view img);
|
void _LoadImage(const std::string_view img);
|
||||||
static std::unordered_map<std::string,Renderable>GFX;
|
static std::unordered_map<std::string,Renderable>GFX;
|
||||||
static std::unordered_map<std::string,Animate2D::Animation<HamsterGame::AnimationState>>ANIMATIONS;
|
static std::unordered_map<std::string,Animate2D::Animation<HamsterGame::AnimationState>>ANIMATIONS;
|
||||||
|
static PixelGameEngine*self;
|
||||||
};
|
};
|
@ -3901,7 +3901,7 @@ namespace olc
|
|||||||
m_tp1 = m_tp2;
|
m_tp1 = m_tp2;
|
||||||
|
|
||||||
// Our time per frame coefficient
|
// Our time per frame coefficient
|
||||||
float fElapsedTime = elapsedTime.count();
|
float fElapsedTime = std::clamp(elapsedTime.count(),0.f,1/30.f);
|
||||||
fLastElapsed = fElapsedTime;
|
fLastElapsed = fElapsedTime;
|
||||||
|
|
||||||
if (bConsoleSuspendTime)
|
if (bConsoleSuspendTime)
|
||||||
|
@ -517,10 +517,10 @@ namespace olc
|
|||||||
namespace olc::utils::geom2d
|
namespace olc::utils::geom2d
|
||||||
{
|
{
|
||||||
// Lemon Meringue
|
// Lemon Meringue
|
||||||
inline const double pi = 3.141592653589793238462643383279502884;
|
inline constexpr double pi = 3.141592653589793238462643383279502884;
|
||||||
|
|
||||||
// Floating point error margin
|
// Floating point error margin
|
||||||
inline const double epsilon = 0.001;
|
inline constexpr double epsilon = 0.001;
|
||||||
|
|
||||||
namespace internal
|
namespace internal
|
||||||
{
|
{
|
||||||
|
31
src/util.cpp
31
src/util.cpp
@ -1,4 +1,5 @@
|
|||||||
#include "util.h"
|
#include "util.h"
|
||||||
|
#include "olcUTIL_Geometry2D.h"
|
||||||
|
|
||||||
std::random_device rd;
|
std::random_device rd;
|
||||||
std::mt19937 rng(rd());
|
std::mt19937 rng(rd());
|
||||||
@ -16,3 +17,33 @@ int olc::util::random(){
|
|||||||
const float olc::util::random_range(const float min,const float max){
|
const float olc::util::random_range(const float min,const float max){
|
||||||
return random(max-min)+min;
|
return random(max-min)+min;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
float olc::util::angle_difference(float angle_1, float angle_2)
|
||||||
|
{
|
||||||
|
angle_1 = fmod(angle_1, 2 * geom2d::pi);
|
||||||
|
angle_2 = fmod(angle_2, 2 * geom2d::pi);
|
||||||
|
float angle_diff = angle_1 - angle_2;
|
||||||
|
|
||||||
|
if (angle_diff > geom2d::pi)
|
||||||
|
angle_diff -= 2 * geom2d::pi;
|
||||||
|
else if (angle_diff < -geom2d::pi)
|
||||||
|
angle_diff += 2 * geom2d::pi;
|
||||||
|
|
||||||
|
return -angle_diff;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
void olc::util::turn_towards_direction(float&angle,float target,float rate)
|
||||||
|
{
|
||||||
|
const auto median_3=[](float a,float b,float c){
|
||||||
|
return std::max(std::min(a, b), std::min(std::max(a, b), c));
|
||||||
|
};
|
||||||
|
|
||||||
|
float diff = angle_difference(angle,target);
|
||||||
|
angle += median_3(-rate, rate, diff);
|
||||||
|
|
||||||
|
float newAngleDiff = angle_difference(angle,target);
|
||||||
|
|
||||||
|
if(diff>0&&newAngleDiff<0||
|
||||||
|
diff<0&&newAngleDiff>0)angle=fmod(target,2*geom2d::pi); //We have crossed the angle difference threshold and can safely say we reached it.
|
||||||
|
}
|
||||||
|
@ -45,4 +45,6 @@ namespace olc::util{
|
|||||||
const float random_range(const float min,const float max);
|
const float random_range(const float min,const float max);
|
||||||
//Returns 0-32767 (as an int).
|
//Returns 0-32767 (as an int).
|
||||||
int random();
|
int random();
|
||||||
|
float angle_difference(float angle_1, float angle_2);
|
||||||
|
void turn_towards_direction(float&angle,float target,float rate);
|
||||||
};
|
};
|
Loading…
x
Reference in New Issue
Block a user