From 01d1e44839dd8738dcd2ff3c3d14a2b77e6bbee7 Mon Sep 17 00:00:00 2001 From: sigonasr2 Date: Fri, 16 Jun 2023 01:41:38 -0500 Subject: [PATCH] Added Bullet shooting --- Crawler/Bullet.cpp | 4 ++++ Crawler/Bullet.h | 11 +++++++++++ Crawler/Crawler.cpp | 26 ++++++++++++++++++++++++++ Crawler/Crawler.h | 1 + Crawler/Crawler.vcxproj | 2 ++ Crawler/Crawler.vcxproj.filters | 6 ++++++ Crawler/DEFINES.h | 3 ++- Crawler/Monster.cpp | 31 +++++++++++++++++++++++++++++-- Crawler/Monster.h | 3 +++ 9 files changed, 84 insertions(+), 3 deletions(-) create mode 100644 Crawler/Bullet.cpp create mode 100644 Crawler/Bullet.h diff --git a/Crawler/Bullet.cpp b/Crawler/Bullet.cpp new file mode 100644 index 00000000..9b1e3dba --- /dev/null +++ b/Crawler/Bullet.cpp @@ -0,0 +1,4 @@ +#include "Bullet.h" + +Bullet::Bullet(vf2d pos,vf2d vel,float radius,int damage,Pixel col) + :pos(pos),vel(vel),radius(radius),damage(damage),col(col){}; \ No newline at end of file diff --git a/Crawler/Bullet.h b/Crawler/Bullet.h new file mode 100644 index 00000000..3a4990dd --- /dev/null +++ b/Crawler/Bullet.h @@ -0,0 +1,11 @@ +#pragma once +#include "olcPixelGameEngine.h" + +struct Bullet{ + vf2d pos; + vf2d vel; + float radius; + int damage; + Pixel col; + Bullet(vf2d pos,vf2d vel,float radius,int damage,Pixel col=WHITE); +}; \ No newline at end of file diff --git a/Crawler/Crawler.cpp b/Crawler/Crawler.cpp index 0efeedbc..44a7ef31 100644 --- a/Crawler/Crawler.cpp +++ b/Crawler/Crawler.cpp @@ -2,6 +2,7 @@ #include "Crawler.h" #include "olcUTIL_Camera2D.h" #include "DamageNumber.h" +#include "Bullet.h" #include "DEFINES.h" //192x192 @@ -10,6 +11,7 @@ std::mapANIMATION_DATA; std::vectorMONSTER_LIST; std::vectorSPAWNER_LIST; std::vectorDAMAGENUMBER_LIST; +std::vectorBULLET_LIST; Crawler*game; Crawler::Crawler() @@ -78,6 +80,7 @@ bool Crawler::OnUserUpdate(float fElapsedTime){ for(Monster&m:MONSTER_LIST){ m.Update(fElapsedTime); } + UpdateBullets(fElapsedTime); UpdateCamera(fElapsedTime); RenderWorld(fElapsedTime); RenderHud(); @@ -322,6 +325,25 @@ void Crawler::UpdateEffects(float fElapsedTime){ } } } +void Crawler::UpdateBullets(float fElapsedTime){ + for(std::vector::iterator it=BULLET_LIST.begin();it!=BULLET_LIST.end();++it){ + Bullet&b=*it; + b.pos+=b.vel*fElapsedTime; + if(geom2d::overlaps(geom2d::circle(player.GetPos(),12*player.GetSizeMult()/2),geom2d::circle(b.pos,b.radius))){ + player.Hurt(b.damage); + it=BULLET_LIST.erase(it); + if(it==BULLET_LIST.end()){ + break; + } + } + if(b.pos.xview.GetWorldBR().x||b.pos.yview.GetWorldBR().y){ + it=BULLET_LIST.erase(it); + if(it==BULLET_LIST.end()){ + break; + } + } + } +} void Crawler::HurtEnemies(vf2d pos,float radius,int damage){ for(Monster&m:MONSTER_LIST){ if(geom2d::overlaps(geom2d::circle(pos,radius),geom2d::circle(m.GetPos(),12*m.GetSizeMult()))){ @@ -362,6 +384,10 @@ void Crawler::RenderWorld(float fElapsedTime){ for(Effect&e:foregroundEffects){ e.Draw(); } + for(Bullet&b:BULLET_LIST){ + view.FillCircle(b.pos,b.radius,b.col); + view.DrawCircle(b.pos,b.radius,WHITE,0xAA); + } for(std::vector::iterator it=DAMAGENUMBER_LIST.begin();it!=DAMAGENUMBER_LIST.end();++it){ DamageNumber&dn=*it; dn.lifeTime+=fElapsedTime; diff --git a/Crawler/Crawler.h b/Crawler/Crawler.h index 73b83322..5d9ef2d6 100644 --- a/Crawler/Crawler.h +++ b/Crawler/Crawler.h @@ -28,6 +28,7 @@ public: void HandleUserInput(float fElapsedTime); void UpdateCamera(float fElapsedTime); void UpdateEffects(float fElapsedTime); + void UpdateBullets(float fElapsedTime); void RenderWorld(float fElapsedTime); void RenderHud(); void AddEffect(Effect foreground,Effect background); diff --git a/Crawler/Crawler.vcxproj b/Crawler/Crawler.vcxproj index 88f5a0c6..f243dfb7 100644 --- a/Crawler/Crawler.vcxproj +++ b/Crawler/Crawler.vcxproj @@ -130,6 +130,7 @@ + @@ -144,6 +145,7 @@ + diff --git a/Crawler/Crawler.vcxproj.filters b/Crawler/Crawler.vcxproj.filters index f51f3b17..324d05a1 100644 --- a/Crawler/Crawler.vcxproj.filters +++ b/Crawler/Crawler.vcxproj.filters @@ -54,6 +54,9 @@ Header Files + + Header Files + @@ -77,6 +80,9 @@ Source Files + + Source Files + diff --git a/Crawler/DEFINES.h b/Crawler/DEFINES.h index 3982e148..ebc4ea2b 100644 --- a/Crawler/DEFINES.h +++ b/Crawler/DEFINES.h @@ -4,4 +4,5 @@ #define INCLUDE_SPAWNER_LIST extern std::vectorSPAWNER_LIST; #define INCLUDE_DAMAGENUMBER_LIST extern std::vectorDAMAGENUMBER_LIST; #define INCLUDE_game extern Crawler*game; -#define INCLUDE_MONSTER_DATA extern std::mapMONSTER_DATA; \ No newline at end of file +#define INCLUDE_MONSTER_DATA extern std::mapMONSTER_DATA; +#define INCLUDE_BULLET_LIST extern std::vectorBULLET_LIST; \ No newline at end of file diff --git a/Crawler/Monster.cpp b/Crawler/Monster.cpp index 4cbdf130..5e884b6a 100644 --- a/Crawler/Monster.cpp +++ b/Crawler/Monster.cpp @@ -1,6 +1,7 @@ #include "Monster.h" #include "DamageNumber.h" #include "Crawler.h" +#include "Bullet.h" #include "DEFINES.h" INCLUDE_ANIMATION_DATA @@ -8,6 +9,7 @@ INCLUDE_MONSTER_DATA INCLUDE_MONSTER_LIST INCLUDE_DAMAGENUMBER_LIST INCLUDE_game +INCLUDE_BULLET_LIST MonsterData::MonsterData(){} MonsterData::MonsterData(MonsterName type,int hp,int atk,std::vectoranimations,float moveSpd,float size,MonsterStrategy strategy,int collisionDmg): @@ -82,6 +84,19 @@ void Monster::PerformJumpAnimation(){ }break; } } +void Monster::PerformShootAnimation(){ + switch(type){ + case SLIME_GREEN:{ + animation.ChangeState(internal_animState,AnimationState::GREEN_SLIME_SPIT); + }break; + case SLIME_BLUE:{ + animation.ChangeState(internal_animState,AnimationState::BLUE_SLIME_SPIT); + }break; + case SLIME_RED:{ + animation.ChangeState(internal_animState,AnimationState::RED_SLIME_SPIT); + }break; + } +} void Monster::SetX(float x){ if(x-12*size>0&&x+12*sizeWORLD_SIZE.x*24){ pos.x=x; @@ -143,8 +158,16 @@ bool Monster::Update(float fElapsedTime){ }break; case SHOOT_AFAR:{ targetAcquireTimer=std::max(0.f,targetAcquireTimer-fElapsedTime); + attackCooldownTimer=std::max(0.f,attackCooldownTimer-fElapsedTime); + if(queueShotTimer>0){ + queueShotTimer-=fElapsedTime; + if(queueShotTimer<0){ + queueShotTimer=0; + BULLET_LIST.push_back(Bullet(pos+vf2d{0,-4},geom2d::line(pos+vf2d{0,-4},game->GetPlayer().GetPos()).vector().norm()*24*3.f,2,atk,{75/2,162/2,225/2})); + } + } geom2d::line line(pos,game->GetPlayer().GetPos()); - if(targetAcquireTimer==0){ + if(targetAcquireTimer==0&&queueShotTimer==0){ targetAcquireTimer=1; if(line.length()<24*6){ target=line.upoint(-1.2); @@ -209,7 +232,11 @@ bool Monster::Update(float fElapsedTime){ PerformJumpAnimation(); }break; default:{ - UpdateAnimation(MONSTER_DATA[type].GetAnimations()[0]); + if(attackCooldownTimer==0){ + attackCooldownTimer=1; + queueShotTimer=0.7; + PerformShootAnimation(); + } } } }break; diff --git a/Crawler/Monster.h b/Crawler/Monster.h index 80850257..fe67ce0b 100644 --- a/Crawler/Monster.h +++ b/Crawler/Monster.h @@ -54,6 +54,8 @@ struct Monster{ int atk; float moveSpd; float size; + float attackCooldownTimer=0; + float queueShotTimer=0; Key facingDirection; MonsterStrategy strategy; State state=State::NORMAL; @@ -88,6 +90,7 @@ struct Monster{ void SetX(float x); void SetY(float y); void PerformJumpAnimation(); + void PerformShootAnimation(); }; struct MonsterSpawner{