Skip to content

Commit

Permalink
Quit reincarnating enemies
Browse files Browse the repository at this point in the history
  • Loading branch information
commandblockguy committed Nov 24, 2020
1 parent 7f21628 commit d8092a9
Show file tree
Hide file tree
Showing 9 changed files with 50 additions and 22 deletions.
1 change: 1 addition & 0 deletions src/globals.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ typedef struct {
uint8_t mineCooldown;
bool player_alive;
uint8_t num_tanks;
bool *alive_tanks;
Tank *player;
} game_t;

Expand Down
23 changes: 18 additions & 5 deletions src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -113,17 +113,20 @@ int main() {
}

bool start_mission(const serialized_tank_t *ser_tanks) {
printf_("starting mission\n");
bool tank_type_used[NUM_TANK_TYPES] = {false};

while(!PhysicsBody::objects.empty()) {
// Delete objects without killing
delete PhysicsBody::objects[0];
}

game.num_tanks = 0;

// todo: don't recreate destroyed tanks
for(uint8_t i = 0; i < game.level.num_tanks; i++) {
new Tank(&ser_tanks[i], i);
if(!game.alive_tanks[i]) continue;
Tank *tank = new Tank(&ser_tanks[i], i);
//printf_("tank created: %p\n", tank);
tank_type_used[ser_tanks[i].type] = true;
}

Expand Down Expand Up @@ -162,16 +165,18 @@ uint8_t play_mission(const serialized_tank_t *ser_tanks) {


profiler_start(physics);
for(auto it: PhysicsBody::objects) {
it->process();
//printf_("0: %p\n", PhysicsBody::objects[0]);
for(auto *it = PhysicsBody::objects.begin(); it < PhysicsBody::objects.end(); it++) {
//printf_("%p\n", *it);
(**it).process();
}
process_collisions();
profiler_end(physics);

if(!game.player_alive) {
profiler_end(total);
game.lives--;
if(!game.lives) {
profiler_end(total);
return LOSE;
} else {
return RETRY;
Expand All @@ -198,9 +203,17 @@ uint8_t play_mission(const serialized_tank_t *ser_tanks) {
uint8_t play_level(const void *comp_tiles, const serialized_tank_t *ser_tanks) {
decompress_tiles(comp_tiles);

bool alive_tanks[32]; // TODO: figure out why dynamic arrays are breaking things
game.alive_tanks = alive_tanks;
for(uint8_t i = 0; i < game.level.num_tanks; i++){
alive_tanks[i] = true;
}

uint8_t status;
do status = play_mission(ser_tanks);
while(status == RETRY);

game.alive_tanks = nullptr;

return status;
}
8 changes: 4 additions & 4 deletions src/mine.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,14 +20,14 @@ void Mine::process() {
if(!countdown) return;

if(--countdown == EXPLOSION_ANIM) {
detonate();
kill();
return;
}

//todo: range detection
}

void Mine::detonate() {
void Mine::kill() {
countdown = EXPLOSION_ANIM - 1;

//The original game uses a radius, not a square
Expand All @@ -44,10 +44,10 @@ void Mine::detonate() {
}
}

// Delete any nearby physics objects
// Kill any nearby physics objects
for(auto *it = objects.begin(); it < objects.end();) {
if(*it != this && center_distance_less_than(*it, MINE_EXPLOSION_RADIUS)) {
delete *it;
(**it).kill();
} else it++;
}

Expand Down
2 changes: 1 addition & 1 deletion src/mine.h
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,8 @@ class Mine: public PhysicsBody {
public:
Mine(Tank *tank);
uint24_t countdown; //Number of physics loops until explosions occur
void detonate();

void kill();
void process();

void handle_collision(PhysicsBody *other);
Expand Down
9 changes: 7 additions & 2 deletions src/physics.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,14 @@ uint24_t PhysicsBody::center_y() const {
}

PhysicsBody::PhysicsBody() {
printf_("PhysicsBody constructor called\n");
objects.push_back(this);
// todo: _vtable gets off-by-one without this
// idek
printf_("pushed %p\n", this);
}

PhysicsBody::~PhysicsBody() {
// Remove from object list
printf_("PhysicsBody destructor called\n");
for(auto *it = objects.begin(); it < objects.end();) {
// Inform any children that we no longer exist
if((**it).parent == this) (**it).parent = nullptr;
Expand All @@ -31,6 +32,10 @@ PhysicsBody::~PhysicsBody() {
}
}

void PhysicsBody::kill() {
delete this;
}

void PhysicsBody::sort() {
// Wikipedia Insertion Sort
for(size_t i = 1; i < objects.size(); i++) {
Expand Down
1 change: 1 addition & 0 deletions src/physics.h
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ class PhysicsBody {

static void sort();

virtual void kill();
virtual void process() = 0;
virtual void render();

Expand Down
14 changes: 7 additions & 7 deletions src/shell.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ void Shell::render() {

bool Shell::ricochet(direction_t dir) {
if(!bounces) {
delete this;
kill();
return false;
}

Expand Down Expand Up @@ -74,17 +74,17 @@ void Shell::handle_collision(PhysicsBody *other) {

void Shell::collide(Tank *tank) {
if(left_tank_hitbox) {
delete tank;
delete this;
tank->kill();
kill();
}
}

void Shell::collide(Shell *shell) {
delete this;
delete shell;
kill();
shell->kill();
}

void Shell::collide(Mine *mine) {
delete this;
mine->detonate();
kill();
mine->kill();
}
11 changes: 9 additions & 2 deletions src/tank.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -49,14 +49,21 @@ Tank::Tank(const serialized_tank_t *ser_tank, uint8_t id) {
}

Tank::~Tank() {
game.num_tanks--;
if(this == game.player) {
game.player_alive = false;
game.player = nullptr;
} else {
}
}

void Tank::kill() {
if(this != game.player) {
game.total_kills++;
game.kills[type]++;
game.alive_tanks[id] = false;
}
game.num_tanks--;

delete this;
}

void Tank::process() {
Expand Down
3 changes: 2 additions & 1 deletion src/tank.h
Original file line number Diff line number Diff line change
Expand Up @@ -62,14 +62,15 @@ class Tank: public PhysicsBody {
ai_move_state_t ai_move;
ai_fire_state_t ai_fire;

// Kill this physics body, then destroy it
void kill();
void process();
void render();
void fire_shell();
void lay_mine();
bool can_shoot() const;
bool can_lay_mine() const;
void set_velocity(int24_t velocity);
bool collide_and_push(Tank *other);

//Number of shots each type of tank can have on-screen at any one time
static const uint8_t max_shells[];
Expand Down

0 comments on commit d8092a9

Please sign in to comment.