Large refactoring and implemented hurt on monsters

pull/28/head
sigonasr2 1 year ago
parent 8bb42df861
commit 6584a240b1
  1. 259
      Crawler/Crawler.cpp
  2. 29
      Crawler/Crawler.h
  3. 5
      Crawler/Crawler.vcxproj
  4. 11
      Crawler/Crawler.vcxproj.filters
  5. 40
      Crawler/Monster.cpp
  6. 25
      Crawler/Monster.h
  7. 6
      Crawler/MonsterData.cpp
  8. 111
      Crawler/Player.cpp
  9. 44
      Crawler/Player.h
  10. 356
      Crawler/main.cpp

@ -0,0 +1,259 @@
#define OLC_PGE_APPLICATION
#include "olcPixelGameEngine.h"
#define OLC_PGEX_TRANSFORMEDVIEW
#include "olcPGEX_TransformedView.h"
#include "Crawler.h"
#include "olcUTIL_Camera2D.h"
//192x192
const vi2d WINDOW_SIZE={24*8,24*8};
std::map<AnimationState,Animate2D::FrameSequence>ANIMATION_DATA;
std::vector<Monster>MONSTER_LIST;
std::vector<MonsterSpawner>SPAWNER_LIST;
Crawler*game;
Crawler::Crawler()
{
sAppName = "Crawler Concept";
game=this;
}
bool Crawler::OnUserCreate(){
//Initialize Camera.
camera=Camera2D{WINDOW_SIZE};
camera.SetMode(olc::utils::Camera2D::Mode::LazyFollow);
camera.SetTarget(player.GetPos());
camera.SetWorldBoundary({0,0},WORLD_SIZE*24);
camera.EnableWorldBoundary(true);
//Graphics
GFX_Pl_Sheet.Load("assets/nico-warrior.png");
GFX_Slime_Sheet.Load("assets/slime.png");
//Animations
InitializeAnimations();
player.AddAnimation(AnimationState::WALK_N);
player.AddAnimation(AnimationState::WALK_E);
player.AddAnimation(AnimationState::WALK_S);
player.AddAnimation(AnimationState::WALK_W);
player.AddAnimation(AnimationState::IDLE_N);
player.AddAnimation(AnimationState::IDLE_E);
player.AddAnimation(AnimationState::IDLE_S);
player.AddAnimation(AnimationState::IDLE_W);
view=TileTransformedView{GetScreenSize(),{1,1}};
player.SetPos({4*24,4*24});
player.UpdateAnimation(AnimationState::IDLE_S);
SPAWNER_LIST.push_back(MonsterSpawner({336,96},4*24,{{{MonsterName::SLIME_BLUE,{-32,-40}},{MonsterName::SLIME_GREEN,{64,20}}}}));
return true;
}
bool Crawler::OnUserUpdate(float fElapsedTime){
HandleUserInput(fElapsedTime);
player.Update(fElapsedTime);
for(Monster&m:MONSTER_LIST){
m.Update(fElapsedTime);
}
UpdateCamera(fElapsedTime);
RenderWorld();
return true;
}
void Crawler::InitializeAnimations(){
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}}});
ANIMATION_DATA[AnimationState::WALK_S]=pl_walk_s;
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}}});
ANIMATION_DATA[AnimationState::WALK_E]=pl_walk_e;
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}}});
ANIMATION_DATA[AnimationState::WALK_W]=pl_walk_w;
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}}});
ANIMATION_DATA[AnimationState::WALK_N]=pl_walk_n;
Animate2D::FrameSequence pl_idle_s;
pl_idle_s.AddFrame({&GFX_Pl_Sheet,{vi2d{0,0}*24,{24,24}}});
ANIMATION_DATA[AnimationState::IDLE_S]=pl_idle_s;
Animate2D::FrameSequence pl_idle_e;
pl_idle_e.AddFrame({&GFX_Pl_Sheet,{vi2d{0,3}*24,{24,24}}});
ANIMATION_DATA[AnimationState::IDLE_E]=pl_idle_e;
Animate2D::FrameSequence pl_idle_w;
pl_idle_w.AddFrame({&GFX_Pl_Sheet,{vi2d{0,2}*24,{24,24}}});
ANIMATION_DATA[AnimationState::IDLE_W]=pl_idle_w;
Animate2D::FrameSequence pl_idle_n;
pl_idle_n.AddFrame({&GFX_Pl_Sheet,{vi2d{0,1}*24,{24,24}}});
ANIMATION_DATA[AnimationState::IDLE_N]=pl_idle_n;
//Load slime animations.
for(int slime=0;slime<4;slime++){
for(int state=0;state<5;state++){
Animate2D::FrameSequence anim;
if(state==4){//These are death animations.
anim=Animate2D::FrameSequence(0.1f,Animate2D::Style::OneShot);
}
for (int frame=0;frame<10;frame++){
anim.AddFrame({&GFX_Slime_Sheet,{vi2d{frame,state+5*slime}*24,{24,24}}});
}
ANIMATION_DATA[AnimationState(AnimationState::GREEN_SLIME_IDLE+state+slime*5)]=anim;
}
}
}
void Crawler::HandleUserInput(float fElapsedTime){
bool setIdleAnimation=true;
if(GetKey(RIGHT).bHeld){
if(player.GetPos().x+12+fElapsedTime*100*player.GetMoveSpdMult()<WORLD_SIZE.x*24){
player.SetX(player.GetX()+fElapsedTime*100*player.GetMoveSpdMult());
} else {
player.SetX(WORLD_SIZE.x*24-12);
}
player.UpdateAnimation(AnimationState::WALK_E);
setIdleAnimation=false;
}
if(GetKey(LEFT).bHeld){
if(player.GetPos().x-12+fElapsedTime*100*player.GetMoveSpdMult()>0){
player.SetX(player.GetX()-fElapsedTime*100*player.GetMoveSpdMult());
} else {
player.SetX(12);
}
if(setIdleAnimation){
player.UpdateAnimation(AnimationState::WALK_W);
}
setIdleAnimation=false;
}
if(GetKey(UP).bHeld){
if(player.GetPos().y-12+fElapsedTime*100*player.GetMoveSpdMult()>0){
player.SetY(player.GetY()-fElapsedTime*100*player.GetMoveSpdMult());
} else {
player.SetY(12);
}
if(setIdleAnimation){
player.UpdateAnimation(AnimationState::WALK_N);
}
setIdleAnimation=false;
}
if(GetKey(DOWN).bHeld){
if(player.GetPos().y+12+fElapsedTime*100*player.GetMoveSpdMult()<WORLD_SIZE.y*24){
player.SetY(player.GetY()+fElapsedTime*100*player.GetMoveSpdMult());
} else {
player.SetY(WORLD_SIZE.y*24-12);
}
if(setIdleAnimation){
player.UpdateAnimation(AnimationState::WALK_S);
}
setIdleAnimation=false;
}
if(GetKey(UP).bReleased){
player.SetLastReleasedMovementKey(UP);
if(GetKey(RIGHT).bHeld){
player.UpdateAnimation(AnimationState::WALK_E);
} else
if(GetKey(DOWN).bHeld){
player.UpdateAnimation(AnimationState::WALK_S);
} else
if(GetKey(LEFT).bHeld){
player.UpdateAnimation(AnimationState::WALK_W);
}
}
if(GetKey(RIGHT).bReleased){
player.SetLastReleasedMovementKey(RIGHT);
if(GetKey(UP).bHeld){
player.UpdateAnimation(AnimationState::WALK_N);
} else
if(GetKey(DOWN).bHeld){
player.UpdateAnimation(AnimationState::WALK_S);
} else
if(GetKey(LEFT).bHeld){
player.UpdateAnimation(AnimationState::WALK_W);
}
}
if(GetKey(LEFT).bReleased){
player.SetLastReleasedMovementKey(LEFT);
if(GetKey(RIGHT).bHeld){
player.UpdateAnimation(AnimationState::WALK_E);
} else
if(GetKey(DOWN).bHeld){
player.UpdateAnimation(AnimationState::WALK_S);
} else
if(GetKey(UP).bHeld){
player.UpdateAnimation(AnimationState::WALK_N);
}
}
if(GetKey(DOWN).bReleased){
player.SetLastReleasedMovementKey(DOWN);
if(GetKey(RIGHT).bHeld){
player.UpdateAnimation(AnimationState::WALK_E);
} else
if(GetKey(UP).bHeld){
player.UpdateAnimation(AnimationState::WALK_N);
} else
if(GetKey(LEFT).bHeld){
player.UpdateAnimation(AnimationState::WALK_W);
}
}
if(setIdleAnimation){
switch(player.GetLastReleasedMovementKey()){
case UP:{
player.UpdateAnimation(AnimationState::IDLE_N);
}break;
case DOWN:{
player.UpdateAnimation(AnimationState::IDLE_S);
}break;
case LEFT:{
player.UpdateAnimation(AnimationState::IDLE_W);
}break;
case RIGHT:{
player.UpdateAnimation(AnimationState::IDLE_E);
}break;
default:{
player.UpdateAnimation(AnimationState::IDLE_S);
}
}
} else {
//We have moved.
player.Moved();
}
}
void Crawler::UpdateCamera(float fElapsedTime){
camera.Update(fElapsedTime);
view.SetWorldOffset(vi2d(camera.GetViewPosition()));
}
void Crawler::RenderWorld(){
Clear({100,180,100});
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++){
view.DrawRect(vi2d{x,y}*24,{24,24},VERY_DARK_GREY);
}
}
view.DrawPartialDecal(player.GetPos()-vi2d{12,12}*player.GetSizeMult(),player.GetFrame().GetSourceImage()->Decal(),player.GetFrame().GetSourceRect().pos,player.GetFrame().GetSourceRect().size,vf2d(player.GetSizeMult(),player.GetSizeMult()));
for(Monster&m:MONSTER_LIST){
view.DrawPartialDecal(m.GetPos()-vi2d{12,12}*m.GetSizeMult(),m.GetFrame().GetSourceImage()->Decal(),m.GetFrame().GetSourceRect().pos,m.GetFrame().GetSourceRect().size,vf2d(m.GetSizeMult(),m.GetSizeMult()));
}
}
int main()
{
Crawler demo;
if (demo.Construct(WINDOW_SIZE.x, WINDOW_SIZE.y, 4, 4))
demo.Start();
return 0;
}

@ -0,0 +1,29 @@
#pragma once
#include "olcPixelGameEngine.h"
#include "Animation.h"
#include "olcUTIL_Animate2D.h"
#include "Monster.h"
#include "olcPGEX_TransformedView.h"
#include "Player.h"
#include "olcUTIL_Camera2D.h"
class Crawler : public olc::PixelGameEngine
{
const vi2d WORLD_SIZE={64,8};
Camera2D camera;
TileTransformedView view;
Player player;
Renderable GFX_Pl_Sheet,GFX_Slime_Sheet;
public:
Crawler();
public:
bool OnUserCreate() override;
bool OnUserUpdate(float fElapsedTime) override;
void InitializeAnimations();
void HandleUserInput(float fElapsedTime);
void UpdateCamera(float fElapsedTime);
void RenderWorld();
};

@ -130,15 +130,18 @@
</ItemDefinitionGroup>
<ItemGroup>
<ClInclude Include="Animation.h" />
<ClInclude Include="Crawler.h" />
<ClInclude Include="Monster.h" />
<ClInclude Include="olcPGEX_TransformedView.h" />
<ClInclude Include="olcPixelGameEngine.h" />
<ClInclude Include="olcUTIL_Animate2D.h" />
<ClInclude Include="olcUTIL_Camera2D.h" />
<ClInclude Include="olcUTIL_Geometry2D.h" />
<ClInclude Include="Player.h" />
</ItemGroup>
<ItemGroup>
<ClCompile Include="main.cpp" />
<ClCompile Include="Crawler.cpp" />
<ClCompile Include="Player.cpp" />
<ClCompile Include="Monster.cpp" />
<ClCompile Include="MonsterData.cpp" />
</ItemGroup>

@ -36,9 +36,15 @@
<ClInclude Include="Animation.h">
<Filter>Header Files</Filter>
</ClInclude>
<ClInclude Include="Crawler.h">
<Filter>Header Files</Filter>
</ClInclude>
<ClInclude Include="Player.h">
<Filter>Header Files</Filter>
</ClInclude>
</ItemGroup>
<ItemGroup>
<ClCompile Include="main.cpp">
<ClCompile Include="Player.cpp">
<Filter>Source Files</Filter>
</ClCompile>
<ClCompile Include="MonsterData.cpp">
@ -47,5 +53,8 @@
<ClCompile Include="Monster.cpp">
<Filter>Source Files</Filter>
</ClCompile>
<ClCompile Include="Crawler.cpp">
<Filter>Source Files</Filter>
</ClCompile>
</ItemGroup>
</Project>

@ -5,8 +5,8 @@ extern std::map<MonsterName,MonsterData>MONSTER_DATA;
extern std::vector<Monster>MONSTER_LIST;
MonsterData::MonsterData(){}
MonsterData::MonsterData(int hp,int atk,std::vector<AnimationState>animations,float moveSpd,float size,MonsterStrategy strategy):
hp(hp),atk(atk),moveSpd(moveSpd),size(size),strategy(strategy),animations(animations){
MonsterData::MonsterData(MonsterName type,int hp,int atk,std::vector<AnimationState>animations,float moveSpd,float size,MonsterStrategy strategy):
type(type),hp(hp),atk(atk),moveSpd(moveSpd),size(size),strategy(strategy),animations(animations){
}
int MonsterData::GetHealth(){
return hp;
@ -20,13 +20,16 @@ float MonsterData::GetMoveSpdMult(){
float MonsterData::GetSizeMult(){
return size;
}
MonsterName MonsterData::GetType(){
return type;
}
MonsterStrategy MonsterData::GetAIStrategy(){
return strategy;
}
Monster::Monster(){}
Monster::Monster(vf2d pos,MonsterData data):
pos(pos),hp(data.GetHealth()),maxhp(data.GetHealth()),atk(data.GetAttack()),moveSpd(data.GetMoveSpdMult()),size(data.GetSizeMult()),strategy(data.GetAIStrategy()){
pos(pos),hp(data.GetHealth()),maxhp(data.GetHealth()),atk(data.GetAttack()),moveSpd(data.GetMoveSpdMult()),size(data.GetSizeMult()),strategy(data.GetAIStrategy()),type(data.GetType()){
bool firstAnimation=true;
for(AnimationState&anim:data.GetAnimations()){
animation.AddState(anim,ANIMATION_DATA[anim]);
@ -58,7 +61,7 @@ Animate2D::Frame Monster::GetFrame(){
void Monster::UpdateAnimation(AnimationState state){
animation.ChangeState(internal_animState,state);
}
void Monster::Update(float fElapsedTime){
bool Monster::Update(float fElapsedTime){
switch(strategy){
RUN_TOWARDS:{
@ -67,8 +70,37 @@ void Monster::Update(float fElapsedTime){
}break;
}
if(hp<=0){
deathTimer+=fElapsedTime;
if(deathTimer>3){
return false;
}
}
animation.UpdateState(internal_animState,randomFrameOffset+fElapsedTime);
randomFrameOffset=0;
return true;
}
AnimationState Monster::GetDeathAnimationName(){
switch(type){
case SLIME_GREEN:{
return AnimationState::GREEN_SLIME_DIE;
}
case SLIME_BLUE:{
return AnimationState::BLUE_SLIME_DIE;
}
case SLIME_RED:{
return AnimationState::RED_SLIME_DIE;
}
default:{
return AnimationState::IDLE_S;
}
}
}
void Monster::Hurt(int damage){
hp=std::max(0,hp-damage);
if(hp<=0){
animation.ChangeState(internal_animState,GetDeathAnimationName());
}
}
MonsterSpawner::MonsterSpawner(){}

@ -1,3 +1,4 @@
#pragma once
#include "olcPixelGameEngine.h"
#include "Animation.h"
#include "olcUTIL_Animate2D.h"
@ -7,6 +8,13 @@ enum MonsterStrategy{
SHOOT_AFAR
};
enum MonsterName{
SLIME_GREEN,
SLIME_BLUE,
SLIME_RED,
SLIME_YELLOW,
};
struct MonsterData{
private:
int hp;
@ -15,27 +23,22 @@ struct MonsterData{
float size;
std::vector<AnimationState> animations;
MonsterStrategy strategy;
MonsterName type;
public:
MonsterData();
//When specifying animations, the first one will become the default animation.
MonsterData(int hp,int atk,std::vector<AnimationState>animations,float moveSpd=1.0f,float size=1.0f,MonsterStrategy strategy=RUN_TOWARDS);
MonsterData(MonsterName type,int hp,int atk,std::vector<AnimationState>animations,float moveSpd=1.0f,float size=1.0f,MonsterStrategy strategy=RUN_TOWARDS);
int GetHealth();
int GetAttack();
float GetMoveSpdMult();
float GetSizeMult();
MonsterName GetType();
MonsterStrategy GetAIStrategy();
std::vector<AnimationState>GetAnimations(){
return animations;
}
};
enum MonsterName{
SLIME_GREEN,
SLIME_BLUE,
SLIME_RED,
SLIME_YELLOW,
};
struct Monster{
private:
vf2d pos;
@ -47,6 +50,9 @@ struct Monster{
Animate2D::Animation<AnimationState>animation;
Animate2D::AnimationState internal_animState;
float randomFrameOffset=0.f;
float deathTimer=0.f;
MonsterName type;
AnimationState GetDeathAnimationName();
public:
Monster();
Monster(vf2d pos,MonsterData data);
@ -57,7 +63,8 @@ struct Monster{
float GetSizeMult();
Animate2D::Frame GetFrame();
void UpdateAnimation(AnimationState state);
void Update(float fElapsedTime);
bool Update(float fElapsedTime);
void Hurt(int damage);
};
struct MonsterSpawner{

@ -3,8 +3,8 @@
#include "Animation.h"
std::map<MonsterName,MonsterData>MONSTER_DATA={
{SLIME_GREEN,MonsterData(10,5,{{AnimationState::GREEN_SLIME_IDLE,AnimationState::GREEN_SLIME_JUMP,AnimationState::GREEN_SLIME_ROLL,AnimationState::GREEN_SLIME_DIE,AnimationState::GREEN_SLIME_SPIT}},1.1f,0.8f,MonsterStrategy::RUN_TOWARDS)},
{SLIME_BLUE,MonsterData(30,10,{{AnimationState::BLUE_SLIME_IDLE,AnimationState::BLUE_SLIME_JUMP,AnimationState::BLUE_SLIME_ROLL,AnimationState::BLUE_SLIME_DIE,AnimationState::BLUE_SLIME_SPIT}},0.8f,1.0f,MonsterStrategy::SHOOT_AFAR)},
{SLIME_RED,MonsterData(25,10,{{AnimationState::RED_SLIME_IDLE,AnimationState::RED_SLIME_JUMP,AnimationState::RED_SLIME_ROLL,AnimationState::RED_SLIME_DIE,AnimationState::RED_SLIME_SPIT}},0.95f,1.2f,MonsterStrategy::RUN_TOWARDS)},
{SLIME_GREEN,MonsterData(MonsterName::SLIME_GREEN,10,5,{{AnimationState::GREEN_SLIME_IDLE,AnimationState::GREEN_SLIME_JUMP,AnimationState::GREEN_SLIME_ROLL,AnimationState::GREEN_SLIME_DIE,AnimationState::GREEN_SLIME_SPIT}},1.1f,0.8f,MonsterStrategy::RUN_TOWARDS)},
{SLIME_BLUE,MonsterData(MonsterName::SLIME_BLUE,30,10,{{AnimationState::BLUE_SLIME_IDLE,AnimationState::BLUE_SLIME_JUMP,AnimationState::BLUE_SLIME_ROLL,AnimationState::BLUE_SLIME_DIE,AnimationState::BLUE_SLIME_SPIT}},0.8f,1.0f,MonsterStrategy::SHOOT_AFAR)},
{SLIME_RED,MonsterData(MonsterName::SLIME_RED,25,10,{{AnimationState::RED_SLIME_IDLE,AnimationState::RED_SLIME_JUMP,AnimationState::RED_SLIME_ROLL,AnimationState::RED_SLIME_DIE,AnimationState::RED_SLIME_SPIT}},0.95f,1.2f,MonsterStrategy::RUN_TOWARDS)},
//{SLIME_YELLOW,{}},
};

@ -0,0 +1,111 @@
#include "Monster.h"
#include "Player.h"
#include "Crawler.h"
extern std::map<MonsterName,MonsterData>MONSTER_DATA;
extern std::vector<Monster>MONSTER_LIST;
extern std::map<AnimationState,Animate2D::FrameSequence>ANIMATION_DATA;
extern std::vector<MonsterSpawner>SPAWNER_LIST;
extern Crawler*game;
Player::Player(){
}
Player::Player(vf2d pos):
pos(pos){
}
void Player::SetX(float x){
pos.x=x;
};
void Player::SetY(float y){
pos.y=y;
}
void Player::SetPos(vf2d pos){
this->pos=pos;
}
vf2d&Player::GetPos(){
return pos;
}
float Player::GetX(){
return pos.x;
}
float Player::GetY(){
return pos.y;
}
int Player::GetHealth(){
return hp;
}
int Player::GetMaxHealth(){
return maxhp;
}
int Player::GetAttack(){
return atk;
}
float Player::GetMoveSpdMult(){
return moveSpd;
}
float Player::GetSizeMult(){
return size;
}
float Player::GetAttackRangeMult(){
return attack_range;
}
void Player::Update(float fElapsedTime){
attack_cooldown_timer=std::max(0.f,attack_cooldown_timer-fElapsedTime);
animation.UpdateState(internal_animState,fElapsedTime);
if(attack_cooldown_timer==0&&game->GetMouse(0).bHeld){
bool attack=false;
for(Monster&m:MONSTER_LIST){
if(geom2d::overlaps(geom2d::circle<float>(pos-vf2d{size*12,size*12},attack_range*size*12),geom2d::circle<float>(m.GetPos()-vf2d{m.GetSizeMult()*12,m.GetSizeMult()*12},m.GetSizeMult()*12))){
m.Hurt(atk);
attack=true;
break;
}
}
if(attack){
attack_cooldown_timer=ATTACK_COOLDOWN;
}
}
}
void Player::AddAnimation(AnimationState state){
animation.AddState(state,ANIMATION_DATA[state]);
}
void Player::UpdateAnimation(AnimationState animState){
animation.ChangeState(internal_animState,animState);
}
Animate2D::Frame Player::GetFrame(){
return animation.GetFrame(internal_animState);
}
void Player::SetLastReleasedMovementKey(Key k){
lastReleasedMovementKey=k;
}
Key Player::GetLastReleasedMovementKey(){
return lastReleasedMovementKey;
}
void Player::Moved(){
for(MonsterSpawner&spawner:SPAWNER_LIST){
if(!spawner.SpawnTriggered()&&geom2d::overlaps(geom2d::circle<float>(pos-vf2d{size*12,size*12},size*12),geom2d::circle<float>(spawner.GetPos(),spawner.GetRange()))){
spawner.SetTriggered(true);
}
}
}

@ -0,0 +1,44 @@
#pragma once
#include "olcUTIL_Animate2D.h"
#include "Animation.h"
#include "Monster.h"
struct Player{
private:
int hp=100,maxhp=hp;
int atk=10;
vf2d pos;
float moveSpd=1.0f;
float size=1.0f;
float attack_range=1.5f;
const float ATTACK_COOLDOWN=0.75f;
float attack_cooldown_timer=0;
Animate2D::Animation<AnimationState>animation;
Animate2D::AnimationState internal_animState;
Key lastReleasedMovementKey;
public:
Player();
Player(vf2d pos);
void SetX(float x);
void SetY(float y);
void SetPos(vf2d pos);
vf2d&GetPos();
float GetX();
float GetY();
int GetHealth();
int GetMaxHealth();
int GetAttack();
float GetMoveSpdMult();
float GetSizeMult();
float GetAttackRangeMult();
void Update(float fElapsedTime);
void AddAnimation(AnimationState state);
void UpdateAnimation(AnimationState animState);
Animate2D::Frame GetFrame();
void SetLastReleasedMovementKey(Key k);
Key GetLastReleasedMovementKey();
//Triggers when the player has moved.
void Moved();
};

@ -1,356 +0,0 @@
#define OLC_PGE_APPLICATION
#include "olcPixelGameEngine.h"
#include "olcUTIL_Camera2D.h"
#define OLC_PGEX_TRANSFORMEDVIEW
#include "olcPGEX_TransformedView.h"
#include "olcUTIL_Animate2D.h"
#include "Monster.h"
#include "Animation.h"
//192x192
const vi2d WINDOW_SIZE={24*8,24*8};
extern std::map<MonsterName,MonsterData>MONSTER_DATA;
std::map<AnimationState,Animate2D::FrameSequence>ANIMATION_DATA;
std::vector<Monster>MONSTER_LIST;
std::vector<MonsterSpawner>SPAWNER_LIST;
struct Player{
private:
int hp=100,maxhp=hp;
int atk=10;
vf2d pos;
float moveSpd=1.0f;
float size=1.0f;
float attack_range=1.5f;
Animate2D::Animation<AnimationState>animation;
Animate2D::AnimationState internal_animState;
Key lastReleasedMovementKey;
public:
Player(){};
Player(vf2d pos):
pos(pos){
};
void SetX(float x){
pos.x=x;
};
void SetY(float y){
pos.y=y;
}
void SetPos(vf2d pos){
this->pos=pos;
}
vf2d&GetPos(){
return pos;
}
float GetX(){
return pos.x;
}
float GetY(){
return pos.y;
}
int GetHealth(){
return hp;
}
int GetMaxHealth(){
return maxhp;
}
int GetAttack(){
return atk;
}
float GetMoveSpdMult(){
return moveSpd;
}
float GetSizeMult(){
return size;
}
float GetAttackRangeMult(){
return attack_range;
}
void Update(float fElapsedTime){
animation.UpdateState(internal_animState,fElapsedTime);
}
void AddAnimation(AnimationState state){
animation.AddState(state,ANIMATION_DATA[state]);
};
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;
}
//Triggers when the player has moved.
void Moved(){
for(MonsterSpawner&spawner:SPAWNER_LIST){
if(!spawner.SpawnTriggered()&&geom2d::overlaps(geom2d::circle<float>(pos,size*12),geom2d::circle<float>(spawner.GetPos(),spawner.GetRange()))){
spawner.SetTriggered(true);
}
}
}
};
class Crawler : public olc::PixelGameEngine
{
const vi2d WORLD_SIZE={64,8};
Camera2D camera;
TileTransformedView view;
Player player=Player{{}};
Renderable GFX_Pl_Sheet,GFX_Slime_Sheet;
public:
Crawler()
{
sAppName = "Crawler Concept";
}
public:
bool OnUserCreate() override
{
//Initialize Camera.
camera=Camera2D{WINDOW_SIZE};
camera.SetMode(olc::utils::Camera2D::Mode::LazyFollow);
camera.SetTarget(player.GetPos());
camera.SetWorldBoundary({0,0},WORLD_SIZE*24);
camera.EnableWorldBoundary(true);
//Graphics
GFX_Pl_Sheet.Load("assets/nico-warrior.png");
GFX_Slime_Sheet.Load("assets/slime.png");
//Animations
InitializeAnimations();
player.AddAnimation(AnimationState::WALK_N);
player.AddAnimation(AnimationState::WALK_E);
player.AddAnimation(AnimationState::WALK_S);
player.AddAnimation(AnimationState::WALK_W);
player.AddAnimation(AnimationState::IDLE_N);
player.AddAnimation(AnimationState::IDLE_E);
player.AddAnimation(AnimationState::IDLE_S);
player.AddAnimation(AnimationState::IDLE_W);
view=TileTransformedView{GetScreenSize(),{1,1}};
player.SetPos({4*24,4*24});
player.UpdateAnimation(AnimationState::IDLE_S);
SPAWNER_LIST.push_back(MonsterSpawner({336,96},4*24,{{{MonsterName::SLIME_GREEN,{-32,-40}},{MonsterName::SLIME_GREEN,{64,20}}}}));
return true;
}
bool OnUserUpdate(float fElapsedTime) override
{
HandleUserInput(fElapsedTime);
player.Update(fElapsedTime);
for(Monster&m:MONSTER_LIST){
m.Update(fElapsedTime);
}
UpdateCamera(fElapsedTime);
RenderWorld();
return true;
}
void InitializeAnimations(){
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}}});
ANIMATION_DATA[AnimationState::WALK_S]=pl_walk_s;
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}}});
ANIMATION_DATA[AnimationState::WALK_E]=pl_walk_e;
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}}});
ANIMATION_DATA[AnimationState::WALK_W]=pl_walk_w;
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}}});
ANIMATION_DATA[AnimationState::WALK_N]=pl_walk_n;
Animate2D::FrameSequence pl_idle_s;
pl_idle_s.AddFrame({&GFX_Pl_Sheet,{vi2d{0,0}*24,{24,24}}});
ANIMATION_DATA[AnimationState::IDLE_S]=pl_idle_s;
Animate2D::FrameSequence pl_idle_e;
pl_idle_e.AddFrame({&GFX_Pl_Sheet,{vi2d{0,3}*24,{24,24}}});
ANIMATION_DATA[AnimationState::IDLE_E]=pl_idle_e;
Animate2D::FrameSequence pl_idle_w;
pl_idle_w.AddFrame({&GFX_Pl_Sheet,{vi2d{0,2}*24,{24,24}}});
ANIMATION_DATA[AnimationState::IDLE_W]=pl_idle_w;
Animate2D::FrameSequence pl_idle_n;
pl_idle_n.AddFrame({&GFX_Pl_Sheet,{vi2d{0,1}*24,{24,24}}});
ANIMATION_DATA[AnimationState::IDLE_N]=pl_idle_n;
for(int slime=0;slime<4;slime++){
for(int state=0;state<5;state++){
Animate2D::FrameSequence anim;
for (int frame=0;frame<10;frame++){
anim.AddFrame({&GFX_Slime_Sheet,{vi2d{frame,state+5*slime}*24,{24,24}}});
}
ANIMATION_DATA[AnimationState(AnimationState::GREEN_SLIME_IDLE+state+slime*5)]=anim;
}
}
}
void HandleUserInput(float fElapsedTime){
bool setIdleAnimation=true;
if(GetKey(RIGHT).bHeld){
if(player.GetPos().x+12+fElapsedTime*100*player.GetMoveSpdMult()<WORLD_SIZE.x*24){
player.SetX(player.GetX()+fElapsedTime*100*player.GetMoveSpdMult());
} else {
player.SetX(WORLD_SIZE.x*24-12);
}
player.UpdateAnimation(AnimationState::WALK_E);
setIdleAnimation=false;
}
if(GetKey(LEFT).bHeld){
if(player.GetPos().x-12+fElapsedTime*100*player.GetMoveSpdMult()>0){
player.SetX(player.GetX()-fElapsedTime*100*player.GetMoveSpdMult());
} else {
player.SetX(12);
}
if(setIdleAnimation){
player.UpdateAnimation(AnimationState::WALK_W);
}
setIdleAnimation=false;
}
if(GetKey(UP).bHeld){
if(player.GetPos().y-12+fElapsedTime*100*player.GetMoveSpdMult()>0){
player.SetY(player.GetY()-fElapsedTime*100*player.GetMoveSpdMult());
} else {
player.SetY(12);
}
if(setIdleAnimation){
player.UpdateAnimation(AnimationState::WALK_N);
}
setIdleAnimation=false;
}
if(GetKey(DOWN).bHeld){
if(player.GetPos().y+12+fElapsedTime*100*player.GetMoveSpdMult()<WORLD_SIZE.y*24){
player.SetY(player.GetY()+fElapsedTime*100*player.GetMoveSpdMult());
} else {
player.SetY(WORLD_SIZE.y*24-12);
}
if(setIdleAnimation){
player.UpdateAnimation(AnimationState::WALK_S);
}
setIdleAnimation=false;
}
if(GetKey(UP).bReleased){
player.SetLastReleasedMovementKey(UP);
if(GetKey(RIGHT).bHeld){
player.UpdateAnimation(AnimationState::WALK_E);
} else
if(GetKey(DOWN).bHeld){
player.UpdateAnimation(AnimationState::WALK_S);
} else
if(GetKey(LEFT).bHeld){
player.UpdateAnimation(AnimationState::WALK_W);
}
}
if(GetKey(RIGHT).bReleased){
player.SetLastReleasedMovementKey(RIGHT);
if(GetKey(UP).bHeld){
player.UpdateAnimation(AnimationState::WALK_N);
} else
if(GetKey(DOWN).bHeld){
player.UpdateAnimation(AnimationState::WALK_S);
} else
if(GetKey(LEFT).bHeld){
player.UpdateAnimation(AnimationState::WALK_W);
}
}
if(GetKey(LEFT).bReleased){
player.SetLastReleasedMovementKey(LEFT);
if(GetKey(RIGHT).bHeld){
player.UpdateAnimation(AnimationState::WALK_E);
} else
if(GetKey(DOWN).bHeld){
player.UpdateAnimation(AnimationState::WALK_S);
} else
if(GetKey(UP).bHeld){
player.UpdateAnimation(AnimationState::WALK_N);
}
}
if(GetKey(DOWN).bReleased){
player.SetLastReleasedMovementKey(DOWN);
if(GetKey(RIGHT).bHeld){
player.UpdateAnimation(AnimationState::WALK_E);
} else
if(GetKey(UP).bHeld){
player.UpdateAnimation(AnimationState::WALK_N);
} else
if(GetKey(LEFT).bHeld){
player.UpdateAnimation(AnimationState::WALK_W);
}
}
if(setIdleAnimation){
switch(player.GetLastReleasedMovementKey()){
case UP:{
player.UpdateAnimation(AnimationState::IDLE_N);
}break;
case DOWN:{
player.UpdateAnimation(AnimationState::IDLE_S);
}break;
case LEFT:{
player.UpdateAnimation(AnimationState::IDLE_W);
}break;
case RIGHT:{
player.UpdateAnimation(AnimationState::IDLE_E);
}break;
default:{
player.UpdateAnimation(AnimationState::IDLE_S);
}
}
} else {
//We have moved.
player.Moved();
}
}
void UpdateCamera(float fElapsedTime){
camera.Update(fElapsedTime);
view.SetWorldOffset(vi2d(camera.GetViewPosition()));
}
void RenderWorld(){
Clear({100,180,100});
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++){
view.DrawRect(vi2d{x,y}*24,{24,24},VERY_DARK_GREY);
}
}
view.DrawPartialDecal(player.GetPos()-vi2d{12,12}*player.GetSizeMult(),player.GetFrame().GetSourceImage()->Decal(),player.GetFrame().GetSourceRect().pos,player.GetFrame().GetSourceRect().size,vf2d(player.GetSizeMult(),player.GetSizeMult()));
for(Monster&m:MONSTER_LIST){
view.DrawPartialDecal(m.GetPos()-vi2d{12,12}*m.GetSizeMult(),m.GetFrame().GetSourceImage()->Decal(),m.GetFrame().GetSourceRect().pos,m.GetFrame().GetSourceRect().size,vf2d(m.GetSizeMult(),m.GetSizeMult()));
}
}
};
int main()
{
Crawler demo;
if (demo.Construct(WINDOW_SIZE.x, WINDOW_SIZE.y, 4, 4))
demo.Start();
return 0;
}
Loading…
Cancel
Save