Player animations and states implemented.

pull/28/head
sigonasr2 1 year ago
parent 3a8db50da8
commit 2d1b8472e1
  1. 1
      Crawler/Crawler.vcxproj
  2. 3
      Crawler/Crawler.vcxproj.filters
  3. BIN
      Crawler/assets/nico-warrior.png
  4. BIN
      Crawler/assets/nico-warrior.xcf
  5. 172
      Crawler/main.cpp
  6. 2
      Crawler/olcUTIL_Animate2D.h
  7. 1046
      Crawler/olcUTIL_Geometry2D.h

@ -133,6 +133,7 @@
<ClInclude Include="olcPixelGameEngine.h" /> <ClInclude Include="olcPixelGameEngine.h" />
<ClInclude Include="olcUTIL_Animate2D.h" /> <ClInclude Include="olcUTIL_Animate2D.h" />
<ClInclude Include="olcUTIL_Camera2D.h" /> <ClInclude Include="olcUTIL_Camera2D.h" />
<ClInclude Include="olcUTIL_Geometry2D.h" />
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>
<ClCompile Include="main.cpp" /> <ClCompile Include="main.cpp" />

@ -27,6 +27,9 @@
<ClInclude Include="olcPGEX_TransformedView.h"> <ClInclude Include="olcPGEX_TransformedView.h">
<Filter>Header Files</Filter> <Filter>Header Files</Filter>
</ClInclude> </ClInclude>
<ClInclude Include="olcUTIL_Geometry2D.h">
<Filter>Header Files</Filter>
</ClInclude>
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>
<ClCompile Include="main.cpp"> <ClCompile Include="main.cpp">

Binary file not shown.

Before

Width:  |  Height:  |  Size: 9.4 KiB

After

Width:  |  Height:  |  Size: 11 KiB

Binary file not shown.

@ -3,18 +3,51 @@
#include "olcUTIL_Camera2D.h" #include "olcUTIL_Camera2D.h"
#define OLC_PGEX_TRANSFORMEDVIEW #define OLC_PGEX_TRANSFORMEDVIEW
#include "olcPGEX_TransformedView.h" #include "olcPGEX_TransformedView.h"
#include "olcUTIL_Animate2D.h"
using namespace olc; using namespace olc;
using namespace olc::utils; using namespace olc::utils;
const vi2d WINDOW_SIZE={24*8,24*8}; const vi2d WINDOW_SIZE={24*8,24*8};
enum AnimationState{
WALK_S,WALK_E,WALK_N,WALK_W,
IDLE_S,IDLE_E,IDLE_N,IDLE_W
};
struct Player{ struct Player{
vf2d pos; vf2d pos;
float moveSpd; float moveSpd;
AnimationState animState=AnimationState::IDLE_S;
private:
Animate2D::Animation<AnimationState>animation;
Animate2D::AnimationState internal_animState;
Key lastReleasedMovementKey;
public:
Player(){}; Player(){};
Player(vf2d pos,float moveSpd): Player(vf2d pos,float moveSpd):
pos(pos),moveSpd(moveSpd){}; pos(pos),moveSpd(moveSpd){
};
void Update(float fElapsedTime){
animation.UpdateState(internal_animState,fElapsedTime);
}
void AddAnimation(AnimationState state,Animate2D::FrameSequence frame){
animation.AddState(state,frame);
};
void UpdateAnimation(AnimationState animState){
animation.ChangeState(internal_animState,animState);
}
Animate2D::Frame GetFrame(){
return animation.GetFrame(internal_animState);
}
void SetLastReleasedMovementKey(Key k){
lastReleasedMovementKey=k;
}
Key GetLastReleasedMovementKey(){
return lastReleasedMovementKey;
}
}; };
class Crawler : public olc::PixelGameEngine class Crawler : public olc::PixelGameEngine
@ -23,6 +56,7 @@ class Crawler : public olc::PixelGameEngine
Camera2D camera; Camera2D camera;
TileTransformedView view; TileTransformedView view;
Player player=Player{{},100}; Player player=Player{{},100};
Renderable GFX_Pl_sheet;
public: public:
Crawler() Crawler()
@ -33,35 +67,167 @@ public:
public: public:
bool OnUserCreate() override bool OnUserCreate() override
{ {
//Initialize Camera.
camera=Camera2D{WINDOW_SIZE}; camera=Camera2D{WINDOW_SIZE};
camera.SetMode(olc::utils::Camera2D::Mode::LazyFollow); camera.SetMode(olc::utils::Camera2D::Mode::LazyFollow);
camera.SetTarget(player.pos); camera.SetTarget(player.pos);
camera.SetWorldBoundary({0,0},WORLD_SIZE*24); camera.SetWorldBoundary({0,0},WORLD_SIZE*24);
camera.EnableWorldBoundary(false); camera.EnableWorldBoundary(false);
//Graphics
GFX_Pl_sheet.Load("assets/nico-warrior.png");
//Animations
Animate2D::FrameSequence pl_walk_s{0.2};
pl_walk_s.AddFrame({&GFX_Pl_sheet,{vi2d{0,0}*24,{24,24}}});
pl_walk_s.AddFrame({&GFX_Pl_sheet,{vi2d{1,0}*24,{24,24}}});
pl_walk_s.AddFrame({&GFX_Pl_sheet,{vi2d{0,0}*24,{24,24}}});
pl_walk_s.AddFrame({&GFX_Pl_sheet,{vi2d{2,0}*24,{24,24}}});
Animate2D::FrameSequence pl_walk_e{0.2};
pl_walk_e.AddFrame({&GFX_Pl_sheet,{vi2d{0,3}*24,{24,24}}});
pl_walk_e.AddFrame({&GFX_Pl_sheet,{vi2d{1,3}*24,{24,24}}});
pl_walk_e.AddFrame({&GFX_Pl_sheet,{vi2d{0,3}*24,{24,24}}});
pl_walk_e.AddFrame({&GFX_Pl_sheet,{vi2d{2,3}*24,{24,24}}});
Animate2D::FrameSequence pl_walk_w{0.2};
pl_walk_w.AddFrame({&GFX_Pl_sheet,{vi2d{0,2}*24,{24,24}}});
pl_walk_w.AddFrame({&GFX_Pl_sheet,{vi2d{1,2}*24,{24,24}}});
pl_walk_w.AddFrame({&GFX_Pl_sheet,{vi2d{0,2}*24,{24,24}}});
pl_walk_w.AddFrame({&GFX_Pl_sheet,{vi2d{2,2}*24,{24,24}}});
Animate2D::FrameSequence pl_walk_n{0.2};
pl_walk_n.AddFrame({&GFX_Pl_sheet,{vi2d{0,1}*24,{24,24}}});
pl_walk_n.AddFrame({&GFX_Pl_sheet,{vi2d{1,1}*24,{24,24}}});
pl_walk_n.AddFrame({&GFX_Pl_sheet,{vi2d{0,1}*24,{24,24}}});
pl_walk_n.AddFrame({&GFX_Pl_sheet,{vi2d{2,1}*24,{24,24}}});
Animate2D::FrameSequence pl_idle_s;
pl_idle_s.AddFrame({&GFX_Pl_sheet,{vi2d{0,0}*24,{24,24}}});
Animate2D::FrameSequence pl_idle_e;
pl_idle_e.AddFrame({&GFX_Pl_sheet,{vi2d{0,3}*24,{24,24}}});
Animate2D::FrameSequence pl_idle_w;
pl_idle_w.AddFrame({&GFX_Pl_sheet,{vi2d{0,2}*24,{24,24}}});
Animate2D::FrameSequence pl_idle_n;
pl_idle_n.AddFrame({&GFX_Pl_sheet,{vi2d{0,1}*24,{24,24}}});
player.AddAnimation(WALK_N,pl_walk_n);
player.AddAnimation(WALK_E,pl_walk_e);
player.AddAnimation(WALK_S,pl_walk_s);
player.AddAnimation(WALK_W,pl_walk_w);
player.AddAnimation(IDLE_N,pl_idle_n);
player.AddAnimation(IDLE_E,pl_idle_e);
player.AddAnimation(IDLE_S,pl_idle_s);
player.AddAnimation(IDLE_W,pl_idle_w);
view=TileTransformedView{GetScreenSize(),{1,1}}; view=TileTransformedView{GetScreenSize(),{1,1}};
player.UpdateAnimation(IDLE_S);
return true; return true;
} }
bool OnUserUpdate(float fElapsedTime) override bool OnUserUpdate(float fElapsedTime) override
{ {
HandleUserInput(fElapsedTime); HandleUserInput(fElapsedTime);
player.Update(fElapsedTime);
UpdateCamera(fElapsedTime); UpdateCamera(fElapsedTime);
RenderWorld(); RenderWorld();
return true; return true;
} }
void HandleUserInput(float fElapsedTime){ void HandleUserInput(float fElapsedTime){
bool setIdleAnimation=true;
if(GetKey(RIGHT).bHeld){ if(GetKey(RIGHT).bHeld){
player.pos.x+=fElapsedTime*player.moveSpd; player.pos.x+=fElapsedTime*player.moveSpd;
player.UpdateAnimation(WALK_E);
setIdleAnimation=false;
} }
if(GetKey(LEFT).bHeld){ if(GetKey(LEFT).bHeld){
player.pos.x-=fElapsedTime*player.moveSpd; player.pos.x-=fElapsedTime*player.moveSpd;
if(setIdleAnimation){
player.UpdateAnimation(WALK_W);
}
setIdleAnimation=false;
} }
if(GetKey(UP).bHeld){ if(GetKey(UP).bHeld){
player.pos.y-=fElapsedTime*player.moveSpd; player.pos.y-=fElapsedTime*player.moveSpd;
if(setIdleAnimation){
player.UpdateAnimation(WALK_N);
}
setIdleAnimation=false;
} }
if(GetKey(DOWN).bHeld){ if(GetKey(DOWN).bHeld){
player.pos.y+=fElapsedTime*player.moveSpd; player.pos.y+=fElapsedTime*player.moveSpd;
if(setIdleAnimation){
player.UpdateAnimation(WALK_S);
}
setIdleAnimation=false;
}
if(GetKey(UP).bReleased){
player.SetLastReleasedMovementKey(UP);
if(GetKey(RIGHT).bHeld){
player.UpdateAnimation(WALK_E);
} else
if(GetKey(DOWN).bHeld){
player.UpdateAnimation(WALK_S);
} else
if(GetKey(LEFT).bHeld){
player.UpdateAnimation(WALK_W);
}
}
if(GetKey(RIGHT).bReleased){
player.SetLastReleasedMovementKey(RIGHT);
if(GetKey(UP).bHeld){
player.UpdateAnimation(WALK_N);
} else
if(GetKey(DOWN).bHeld){
player.UpdateAnimation(WALK_S);
} else
if(GetKey(LEFT).bHeld){
player.UpdateAnimation(WALK_W);
}
}
if(GetKey(LEFT).bReleased){
player.SetLastReleasedMovementKey(LEFT);
if(GetKey(RIGHT).bHeld){
player.UpdateAnimation(WALK_E);
} else
if(GetKey(DOWN).bHeld){
player.UpdateAnimation(WALK_S);
} else
if(GetKey(UP).bHeld){
player.UpdateAnimation(WALK_N);
}
}
if(GetKey(DOWN).bReleased){
player.SetLastReleasedMovementKey(DOWN);
if(GetKey(RIGHT).bHeld){
player.UpdateAnimation(WALK_E);
} else
if(GetKey(UP).bHeld){
player.UpdateAnimation(WALK_N);
} else
if(GetKey(LEFT).bHeld){
player.UpdateAnimation(WALK_W);
}
}
if(setIdleAnimation){
switch(player.GetLastReleasedMovementKey()){
case UP:{
player.UpdateAnimation(IDLE_N);
}break;
case DOWN:{
player.UpdateAnimation(IDLE_S);
}break;
case LEFT:{
player.UpdateAnimation(IDLE_W);
}break;
case RIGHT:{
player.UpdateAnimation(IDLE_E);
}break;
default:{
player.UpdateAnimation(IDLE_S);
}
}
} }
} }
@ -71,13 +237,13 @@ public:
} }
void RenderWorld(){ void RenderWorld(){
Clear(BLACK); Clear({100,180,100});
for (int x = view.GetTopLeftTile().x/24-1; x <= view.GetBottomRightTile().x/24; x++){ for (int x = view.GetTopLeftTile().x/24-1; x <= view.GetBottomRightTile().x/24; x++){
for (int y = view.GetTopLeftTile().y/24-1; y <= view.GetBottomRightTile().y/24; y++){ for (int y = view.GetTopLeftTile().y/24-1; y <= view.GetBottomRightTile().y/24; y++){
view.DrawRect(vi2d{x,y}*24,{24,24},VERY_DARK_GREY); view.DrawRect(vi2d{x,y}*24,{24,24},VERY_DARK_GREY);
} }
} }
view.DrawCircle(player.pos,8); view.DrawPartialDecal(player.pos-vi2d{12,12},player.GetFrame().GetSourceImage()->Decal(),player.GetFrame().GetSourceRect().pos,player.GetFrame().GetSourceRect().size);
} }
}; };

@ -55,7 +55,7 @@ David Barr, aka javidx9, <EFBFBD>OneLoneCoder 2019, 2020, 2021, 2022
#pragma once #pragma once
#include "olcPixelGameEngine.h" #include "olcPixelGameEngine.h"
#include "utilities/olcUTIL_Geometry2D.h" #include "olcUTIL_Geometry2D.h"
namespace olc::utils::Animate2D namespace olc::utils::Animate2D
{ {

File diff suppressed because it is too large Load Diff
Loading…
Cancel
Save