Enemies shoot back!

This commit is contained in:
stevenhowes
2021-03-21 00:20:36 +00:00
parent 276bd46e2b
commit 5081aa5a6a
2 changed files with 93 additions and 40 deletions
BIN
View File
Binary file not shown.
+93 -40
View File
@@ -55,6 +55,8 @@ struct NPC_s {
int collideforce; int collideforce;
int collidable; int collidable;
int explodenextframe; int explodenextframe;
int nextfire;
int fireinterval;
}; };
struct Star_s { struct Star_s {
@@ -70,6 +72,7 @@ struct Player_s {
unsigned char velocity; unsigned char velocity;
int nextidlesprite; int nextidlesprite;
int shields; int shields;
int nextshieldheal;
int integrity; int integrity;
int remainingdistance; int remainingdistance;
struct EntityLocation_s hitbox_bl; struct EntityLocation_s hitbox_bl;
@@ -172,6 +175,51 @@ void intro()
} }
} }
void game_spawn_projectile(int id, int Px, int Py, int Vx, int Vy,enum sprite_e sprite, int damage)
{
int i;
if(id < 0)
{
for(i = 0; i < MAX_PROJECTILES; i++)
{
if(Projectiles[i].active == 0)
id = i;
}
}
// If no velocity X specified we're targetting the player
if(Vx == 0)
{
int Xdistance = abs((Player.location.X + (Player.hitbox_tr.X/2)) - Px);
int Ydistance = abs((Player.location.Y + (Player.hitbox_tr.Y/2)) - Py);
int distance = sqrt((Xdistance^2) + (Ydistance^2));
Vx = Xdistance / (distance / 10);
Vy = Ydistance / (distance / 10);
if(Player.location.X > Px)
Vx = 0 - Vx;
}
// If it's too close to horizontal you can't dodge
if(Vy < 2)
id = -1;
//If no free IDs then we go without
if(id >= 0)
{
Projectiles[id].location.X = Px;
Projectiles[id].location.Y = Py;
Projectiles[id].velocity.X = Vx;
Projectiles[id].velocity.Y = Vy;
Projectiles[id].active = 1;
Projectiles[id].sprite = sprite;
Projectiles[id].nextframe = tick + 10;
Projectiles[id].damage = damage;
Projectiles[id].collidable = 1;
}
}
void game_draw_player() void game_draw_player()
{ {
draw_sprite(sprites[Player.sprite], Player.location.X, Player.location.Y); draw_sprite(sprites[Player.sprite], Player.location.X, Player.location.Y);
@@ -340,6 +388,8 @@ void game_respawn_npc(int id)
NPCS[id].location.Y = DISPLAY_Y + (rand() % (DISPLAY_Y/2)); NPCS[id].location.Y = DISPLAY_Y + (rand() % (DISPLAY_Y/2));
NPCS[id].npctype = rand() % (maxnpctype); NPCS[id].npctype = rand() % (maxnpctype);
NPCS[id].collidable = 1; NPCS[id].collidable = 1;
NPCS[id].fireinterval = 200;
NPCS[id].nextfire = tick + (rand() % 100);
switch(NPCS[id].npctype) switch(NPCS[id].npctype)
{ {
case bigdurno: case bigdurno:
@@ -362,7 +412,7 @@ void game_respawn_npc(int id)
NPCS[id].hitbox_bl.Y = 0; NPCS[id].hitbox_bl.Y = 0;
NPCS[id].hitbox_tr.X = 38; NPCS[id].hitbox_tr.X = 38;
NPCS[id].hitbox_tr.Y = 56; NPCS[id].hitbox_tr.Y = 56;
NPCS[id].collideforce = 30; NPCS[id].collideforce = 50;
break; break;
}; };
} }
@@ -392,6 +442,13 @@ void game_tick_npcs()
if((NPCS[i].location.X + NPCS[i].hitbox_bl.X) < 0) if((NPCS[i].location.X + NPCS[i].hitbox_bl.X) < 0)
game_respawn_npc(i); game_respawn_npc(i);
if(tick > NPCS[i].nextfire)
{
// Dont fire if off screen
if((NPCS[i].location.Y < DISPLAY_Y))
game_spawn_projectile(-1,NPCS[i].location.X, NPCS[i].location.Y, 0, NPCS[i].velocity.Y, photon1, 30);
NPCS[i].nextfire = tick + NPCS[i].fireinterval;
}
} }
} }
@@ -489,6 +546,32 @@ void game_collider_tick()
} }
} }
} }
for(i = 0; i < MAX_PROJECTILES; i++)
{
if(!Projectiles[i].active)
continue;
if(!NPCS[i].collidable)
continue;
// Excuse this formatting
if(
game_hitbox_collide(
(Player.location.X + Player.hitbox_bl.X),(Player.location.Y + Player.hitbox_bl.Y),
(Player.hitbox_tr.X - Player.hitbox_bl.X),(Player.hitbox_tr.Y - Player.hitbox_bl.Y),
Projectiles[i].location.X,Projectiles[i].location.Y,
10,10
)
)
{
Projectiles[i].active = 0;
Player.shields -= Projectiles[i].damage;
if(Player.shields < 0)
{
Player.integrity += Player.shields;
Player.shields = 0;
}
}
}
} }
int game_hitbox_collide(int x1, int y1, int w1, int h1, int x2, int y2, int w2, int h2) int game_hitbox_collide(int x1, int y1, int w1, int h1, int x2, int y2, int w2, int h2)
@@ -532,46 +615,7 @@ void game_projectiles_tick()
} }
} }
void game_spawn_projectile(int id,int Px,int Py, int Vx,int Vy,enum sprite_e sprite, int damage)
{
int i;
if(id < 0)
{
for(i = 0; i < MAX_PROJECTILES; i++)
{
if(Projectiles[i].active == 0)
id = i;
}
}
// If no velocity X specified we're targetting the player
if(Vx == 0)
{
int Xdistance = abs(Player.location.X - Px);
int Ydistance = abs(Player.location.Y - Py);
int distance = sqrt((Xdistance^2) + (Ydistance^2));
Vx = Xdistance / (distance / 10);
Vy = Ydistance / (distance / 10);
if(Player.location.X > Px)
Vx = 0 - Vx;
}
//If no free IDs then we go without
if(id >= 0)
{
Projectiles[id].location.X = Px;
Projectiles[id].location.Y = Py;
Projectiles[id].velocity.X = Vx;
Projectiles[id].velocity.Y = Vy;
Projectiles[id].active = 1;
Projectiles[id].sprite = sprite;
Projectiles[id].nextframe = tick + 10;
Projectiles[id].damage = damage;
Projectiles[id].collidable = 1;
}
}
void game_input_tick() void game_input_tick()
{ {
@@ -685,6 +729,15 @@ void game_setup()
void game_tick_player() void game_tick_player()
{ {
Player.remainingdistance -= Player.velocity * (tick - lasttick); Player.remainingdistance -= Player.velocity * (tick - lasttick);
if(Player.shields < 100)
{
if(tick > Player.nextshieldheal)
{
Player.shields += 1;
Player.nextshieldheal = tick + 10;
}
}
} }
void game_tick() void game_tick()