Added Bullet shooting
This commit is contained in:
parent
a2308914a2
commit
01d1e44839
4
Crawler/Bullet.cpp
Normal file
4
Crawler/Bullet.cpp
Normal file
@ -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){};
|
11
Crawler/Bullet.h
Normal file
11
Crawler/Bullet.h
Normal file
@ -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);
|
||||
};
|
@ -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::map<AnimationState,Animate2D::FrameSequence>ANIMATION_DATA;
|
||||
std::vector<Monster>MONSTER_LIST;
|
||||
std::vector<MonsterSpawner>SPAWNER_LIST;
|
||||
std::vector<DamageNumber>DAMAGENUMBER_LIST;
|
||||
std::vector<Bullet>BULLET_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<Bullet>::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.x<view.GetWorldTL().x||b.pos.x>view.GetWorldBR().x||b.pos.y<view.GetWorldTL().y||b.pos.y>view.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<DamageNumber>::iterator it=DAMAGENUMBER_LIST.begin();it!=DAMAGENUMBER_LIST.end();++it){
|
||||
DamageNumber&dn=*it;
|
||||
dn.lifeTime+=fElapsedTime;
|
||||
|
@ -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);
|
||||
|
@ -130,6 +130,7 @@
|
||||
</ItemDefinitionGroup>
|
||||
<ItemGroup>
|
||||
<ClInclude Include="Animation.h" />
|
||||
<ClInclude Include="Bullet.h" />
|
||||
<ClInclude Include="Crawler.h" />
|
||||
<ClInclude Include="DamageNumber.h" />
|
||||
<ClInclude Include="DEFINES.h" />
|
||||
@ -144,6 +145,7 @@
|
||||
<ClInclude Include="State.h" />
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<ClCompile Include="Bullet.cpp" />
|
||||
<ClCompile Include="Crawler.cpp" />
|
||||
<ClCompile Include="DamageNumber.cpp" />
|
||||
<ClCompile Include="Effect.cpp" />
|
||||
|
@ -54,6 +54,9 @@
|
||||
<ClInclude Include="DEFINES.h">
|
||||
<Filter>Header Files</Filter>
|
||||
</ClInclude>
|
||||
<ClInclude Include="Bullet.h">
|
||||
<Filter>Header Files</Filter>
|
||||
</ClInclude>
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<ClCompile Include="Player.cpp">
|
||||
@ -77,6 +80,9 @@
|
||||
<ClCompile Include="Effect.cpp">
|
||||
<Filter>Source Files</Filter>
|
||||
</ClCompile>
|
||||
<ClCompile Include="Bullet.cpp">
|
||||
<Filter>Source Files</Filter>
|
||||
</ClCompile>
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<None Include="cpp.hint" />
|
||||
|
@ -5,3 +5,4 @@
|
||||
#define INCLUDE_DAMAGENUMBER_LIST extern std::vector<DamageNumber>DAMAGENUMBER_LIST;
|
||||
#define INCLUDE_game extern Crawler*game;
|
||||
#define INCLUDE_MONSTER_DATA extern std::map<MonsterName,MonsterData>MONSTER_DATA;
|
||||
#define INCLUDE_BULLET_LIST extern std::vector<Bullet>BULLET_LIST;
|
@ -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::vector<AnimationState>animations,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*size<game->WORLD_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;
|
||||
|
@ -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{
|
||||
|
Loading…
x
Reference in New Issue
Block a user