Skip to content
Open
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 12 additions & 6 deletions games/BelowTheSurface/behaviour.h
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,7 @@ class BlobBehaviour : public Behaviour
this->blob_machine->set_facing_left(facing_left);
fall_to_ground();
this->blob_machine->update();
this->blob_machine->apply_next_state();
};
};

Expand Down Expand Up @@ -180,6 +181,7 @@ class SnakeBehaviour : public Behaviour
this->snake_machine->set_facing_left(facing_left);
fall_to_ground();
this->snake_machine->update();
this->snake_machine->apply_next_state();
};

void face_player()
Expand All @@ -190,8 +192,8 @@ class SnakeBehaviour : public Behaviour
if(change_direction)
continue;

point_2d player_center = to_screen(center_point(level_players[i]->get_player_sprite()));
point_2d enemy_center = to_screen(center_point(enemy_sprite));
point_2d player_center = to_screen(sprite_center_point(level_players[i]->get_player_sprite()));
point_2d enemy_center = to_screen(sprite_center_point(enemy_sprite));

double dist = player_center.x - enemy_center.x;

Expand Down Expand Up @@ -238,6 +240,7 @@ class RatBehaviour : public Behaviour
this->rat_machine->set_facing_left(facing_left);
fall_to_ground();
this->rat_machine->update();
this->rat_machine->apply_next_state();
};

void random_actions()
Expand All @@ -252,7 +255,7 @@ class RatBehaviour : public Behaviour

if(choice1 > 990)
{
this->rat_machine->change_state(new RatIdle, "Idle");
this->rat_machine->request_state_change(new RatIdle, "Idle");
}
if(choice2 > 980)
{
Expand Down Expand Up @@ -288,6 +291,7 @@ class WaterRatBehaviour : public Behaviour
this->boss_machine->set_facing_left(facing_left);
fall_to_ground();
this->boss_machine->update();
this->boss_machine->apply_next_state();
};

void backwards_behaviour()
Expand All @@ -313,8 +317,8 @@ class WaterRatBehaviour : public Behaviour

void face_player()
{
point_2d player_center = to_screen(center_point(level_players[0]->get_player_sprite()));
point_2d enemy_center = to_screen(center_point(enemy_sprite));
point_2d player_center = to_screen(sprite_center_point(level_players[0]->get_player_sprite()));
point_2d enemy_center = to_screen(sprite_center_point(enemy_sprite));

if(enemy_center.x < player_center.x)
facing_left = true;
Expand All @@ -331,7 +335,7 @@ class WaterRatBehaviour : public Behaviour
{
if(state == 0)
{
this->boss_machine->change_state(new BossDying, "Dying");
this->boss_machine->request_state_change(new BossDying, "Dying");
}
}
};
Expand Down Expand Up @@ -374,6 +378,7 @@ class FlyBehaviour : public Behaviour
this->fly_machine->set_facing_left(facing_left);
this->fly_machine->set_flying_up(flying_up);
this->fly_machine->update();
this->fly_machine->apply_next_state();

if(type == "Purp")
face_random_direction();
Expand Down Expand Up @@ -425,6 +430,7 @@ class TentacleBehaviour : public Behaviour
this->tentacle_machine->set_facing_left(facing_left);
fall_to_ground();
this->tentacle_machine->update();
this->tentacle_machine->apply_next_state();
};

};
17 changes: 17 additions & 0 deletions games/BelowTheSurface/blobmachine.h
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,8 @@ class BlobMachine
{
private:
BlobMachineState *state;
BlobMachineState *next_state;
string next_state_type;
sprite enemy_sprite;
bool facing_left;
vector<std::shared_ptr<Player>> level_players;
Expand All @@ -53,13 +55,28 @@ class BlobMachine
delete state;
};

void request_state_change(BlobMachineState *new_state, string type)
{
this->next_state = new_state;
this->next_state_type = type;
};

void change_state(BlobMachineState *new_state, string type)
{
if (this->state != nullptr)
delete this->state;
this->state = new_state;
this->state->set_state(this, type);
};

void apply_next_state()
{
if (this->next_state != nullptr)
{
change_state(this->next_state, this->next_state_type);
this->next_state = nullptr;
}
}

void update()
{
Expand Down
55 changes: 36 additions & 19 deletions games/BelowTheSurface/bossmachine.h
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,8 @@ class BossMachine
{
private:
BossMachineState *state;
BossMachineState *next_state;
string next_state_type;
sprite enemy_sprite;
bool facing_left;
vector<std::shared_ptr<Player>> level_players;
Expand All @@ -52,6 +54,12 @@ class BossMachine
{
delete state;
};

void request_state_change(BossMachineState *new_state, string type)
{
this->next_state = new_state;
this->next_state_type = type;
};

void change_state(BossMachineState *new_state, string type)
{
Expand All @@ -60,6 +68,15 @@ class BossMachine
this->state = new_state;
this->state->set_state(this, type);
};

void apply_next_state()
{
if (this->next_state != nullptr)
{
change_state(this->next_state, this->next_state_type);
this->next_state = nullptr;
}
}

void update()
{
Expand Down Expand Up @@ -244,7 +261,7 @@ void BossIdle::update()
//double y_dist = boss_pos.y - player_pos.y;
if(abs(x_dist) < 300)
{
this->boss->change_state(new BossRise(0), "Rise");
this->boss->request_state_change(new BossRise(0), "Rise");
break;
}
}
Expand Down Expand Up @@ -283,12 +300,12 @@ void BossMove::update()
if(choice < 80)
{
sprite_set_dx(boss_sprite, 0);
this->boss->change_state(new BossRise(1), "RiseAttack");
this->boss->request_state_change(new BossRise(1), "RiseAttack");
break;
}
else
{
this->boss->change_state(new BossMoveBackwards, "MoveBackwards");
this->boss->request_state_change(new BossMoveBackwards, "MoveBackwards");
}
}
}
Expand Down Expand Up @@ -337,15 +354,15 @@ void BossMoveBackwards::update()

if(choice < 30)
{
this->boss->change_state(new BossRise(0), "Rise");
this->boss->request_state_change(new BossRise(0), "Rise");
}
else if(choice > 33 && choice < 66)
{
this->boss->change_state(new BossSpotPlayer, "Spot");
this->boss->request_state_change(new BossSpotPlayer, "Spot");
}
else
{
this->boss->change_state(new BossMove, "Move");
this->boss->request_state_change(new BossMove, "Move");
}
}
}
Expand All @@ -365,9 +382,9 @@ void BossRise::update()
if(sprite_animation_has_ended(boss_sprite))
{
if(rise_type == 0)
this->boss->change_state(new BossBattleCry(3), "BattleCry");
this->boss->request_state_change(new BossBattleCry(3), "BattleCry");
if(rise_type == 1)
this->boss->change_state(new BossAttack, "Attack");
this->boss->request_state_change(new BossAttack, "Attack");
}
}

Expand All @@ -384,7 +401,7 @@ void BossSpotPlayer::update()
set_proper_direction(boss_sprite, "RightSpotPlayer");

if(sprite_animation_has_ended(boss_sprite))
this->boss->change_state(new BossDescend(0), "Descend");
this->boss->request_state_change(new BossDescend(0), "Descend");
}

void BossBattleCry::update()
Expand All @@ -401,13 +418,13 @@ void BossBattleCry::update()
if(sprite_animation_has_ended(boss_sprite))
{
if(battle_cry_type == 0)
this->boss->change_state(new BossSpotPlayer, "SpotPlayer");
this->boss->request_state_change(new BossSpotPlayer, "SpotPlayer");
if(battle_cry_type == 1)
this->boss->change_state(new BossAttack, "Attack");
this->boss->request_state_change(new BossAttack, "Attack");
if(battle_cry_type == 2)
this->boss->change_state(new BossDescend(1), "Descend");
this->boss->request_state_change(new BossDescend(1), "Descend");
if(battle_cry_type == 3)
this->boss->change_state(new BossDescend(0), "Descend");
this->boss->request_state_change(new BossDescend(0), "Descend");
}
}

Expand Down Expand Up @@ -437,19 +454,19 @@ void BossAttack::update()
double choice = dist(mt);
if(choice < 30)
{
this->boss->change_state(new BossDescend(1), "Descend");
this->boss->request_state_change(new BossDescend(1), "Descend");
}
else if(choice > 30 && choice < 50)
{
this->boss->change_state(new BossDescend(0), "Descend");
this->boss->request_state_change(new BossDescend(0), "Descend");
}
else if(choice > 50 && choice < 90)
{
this->boss->change_state(new BossAttack, "Attack");
this->boss->request_state_change(new BossAttack, "Attack");
}
else
{
this->boss->change_state(new BossBattleCry(2), "BattleCry");
this->boss->request_state_change(new BossBattleCry(2), "BattleCry");
}
}
}
Expand All @@ -466,9 +483,9 @@ void BossDescend::update()
if(sprite_animation_has_ended(boss_sprite))
{
if(lower_type == 0)
this->boss->change_state(new BossMove, "Move");
this->boss->request_state_change(new BossMove, "Move");
if(lower_type == 1)
this->boss->change_state(new BossMoveBackwards, "MoveBackwards");
this->boss->request_state_change(new BossMoveBackwards, "MoveBackwards");
}

}
Expand Down
10 changes: 5 additions & 5 deletions games/BelowTheSurface/collision.h
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ void check_solid_block_collisions(vector<vector<shared_ptr<Block>>> solid_blocks
level_players[k]->set_player_dy(0);
level_players[k]->set_on_floor(false);
sprite_set_y(level_players[k]->get_player_sprite(), sprite_y(level_players[k]->get_player_sprite()) + 5);
level_players[k]->change_state(new JumpFallState, "JumpFall");
level_players[k]->request_state_change(new JumpFallState, "JumpFall");
break;
}
else if (collision == "Left")
Expand Down Expand Up @@ -158,7 +158,7 @@ void check_door_block_collisions(shared_ptr<DoorBlock> door, vector<shared_ptr<P
{
level_players[i]->set_player_won(true);
door->open_portal();
level_players[i]->change_state(new DanceState, "Dance");
level_players[i]->request_state_change(new DanceState, "Dance");
}
}
}
Expand Down Expand Up @@ -221,7 +221,7 @@ void check_ladder_collisions(vector<vector<shared_ptr<Ladder>>> ladders, unorder
level_players[k]->set_on_ladder(true);
sprite_set_y(level_players[k]->get_player_sprite(), sprite_y(level_players[k]->get_player_sprite()) - 10);
level_players[k]->set_player_dx(0);
level_players[k]->change_state(new ClimbState, "Climb");
level_players[k]->request_state_change(new ClimbState, "Climb");
level_players[k]->set_on_floor(false);
break;
}
Expand Down Expand Up @@ -383,7 +383,7 @@ void check_enemy_player_collisions(vector<shared_ptr<Enemy>> level_enemies, vect
{
level_players[j]->player_health -= 1;
start_timer(damage_timer);
level_players[j]->change_state(new HurtState, "Hurt");
level_players[j]->request_state_change(new HurtState, "Hurt");
}

int time = timer_ticks(damage_timer) / 1000;
Expand Down Expand Up @@ -416,7 +416,7 @@ void check_enemy_player_collisions(vector<shared_ptr<Enemy>> level_enemies, vect
level_enemies[i]->take_damage(1);
}
}
level_players[j]->change_state(new JumpRiseState, "JumpRise");
level_players[j]->request_state_change(new JumpRiseState, "JumpRise");
level_players[j]->set_player_dx(0);
}
}
Expand Down
17 changes: 17 additions & 0 deletions games/BelowTheSurface/flymachine.h
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,8 @@ class FlyMachine
{
private:
FlyMachineState *state;
FlyMachineState *next_state;
string next_state_type;
sprite enemy_sprite;
bool facing_left;
bool flying_up = true;
Expand All @@ -51,6 +53,12 @@ class FlyMachine
{
delete state;
};

void request_state_change(FlyMachineState *new_state, string type)
{
this->next_state = new_state;
this->next_state_type = type;
};

void change_state(FlyMachineState *new_state, string type)
{
Expand All @@ -59,6 +67,15 @@ class FlyMachine
this->state = new_state;
this->state->set_state(this, type);
};

void apply_next_state()
{
if (this->next_state != nullptr)
{
change_state(this->next_state, this->next_state_type);
this->next_state = nullptr;
}
}

void update()
{
Expand Down
Loading