Cherry
This commit is contained in:
parent
ebc3d155de
commit
c67ddcc6d2
8 changed files with 71 additions and 14 deletions
|
|
@ -26,7 +26,6 @@ class Entity{
|
||||||
|
|
||||||
|
|
||||||
class Pacman : public Entity{
|
class Pacman : public Entity{
|
||||||
const int speed = 16;
|
|
||||||
const int frame_count = 7;
|
const int frame_count = 7;
|
||||||
const int fps = 24;
|
const int fps = 24;
|
||||||
private:
|
private:
|
||||||
|
|
@ -70,13 +69,14 @@ class Scorepoint : public Entity {
|
||||||
};
|
};
|
||||||
|
|
||||||
class Ghost : public Entity {
|
class Ghost : public Entity {
|
||||||
|
const int respawn_time = 45;
|
||||||
private:
|
private:
|
||||||
static unsigned int color_decision;
|
static unsigned int color_decision;
|
||||||
TextureAtlas texture;
|
TextureAtlas texture;
|
||||||
const int speed = 16;
|
|
||||||
Color color;
|
Color color;
|
||||||
int direction = 1;
|
int direction = 1;
|
||||||
Vector2 start_position;
|
Vector2 start_position;
|
||||||
|
int respawn_timer = 0;
|
||||||
void recalculate_direction();
|
void recalculate_direction();
|
||||||
void try_to_chase();
|
void try_to_chase();
|
||||||
public:
|
public:
|
||||||
|
|
@ -87,4 +87,12 @@ class Ghost : public Entity {
|
||||||
void collision(Entity* with) override;
|
void collision(Entity* with) override;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
class Cherry : public Entity {
|
||||||
|
private:
|
||||||
|
TextureAtlas texture;
|
||||||
|
public:
|
||||||
|
Cherry();
|
||||||
|
void draw() const override;
|
||||||
|
};
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
|
|
||||||
|
|
@ -35,6 +35,11 @@ void Ghost::tick() {
|
||||||
|
|
||||||
World& world = get_world();
|
World& world = get_world();
|
||||||
|
|
||||||
|
if(respawn_timer>0){
|
||||||
|
respawn_timer--;
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
if(dynamic_cast<GhostWall*>(world.grid[world.indexify_position(check_position)]) != nullptr){
|
if(dynamic_cast<GhostWall*>(world.grid[world.indexify_position(check_position)]) != nullptr){
|
||||||
recalculate_direction();
|
recalculate_direction();
|
||||||
check_position = project_position(direction, 1);
|
check_position = project_position(direction, 1);
|
||||||
|
|
@ -80,9 +85,22 @@ void Ghost::recalculate_direction(){
|
||||||
}
|
}
|
||||||
|
|
||||||
void Ghost::draw() const {
|
void Ghost::draw() const {
|
||||||
DrawTextureRec(this->texture.get_texture(), this->texture.rect_view(0,0,16, 16), this->position, this->color);
|
if (get_world().get_killmode() == true || respawn_timer > 0)
|
||||||
|
DrawTextureRec(texture.get_texture(), texture.rect_view(16, 0, 16, 16), position, WHITE);
|
||||||
|
else
|
||||||
|
DrawTextureRec(this->texture.get_texture(), this->texture.rect_view(0,0,16, 16), this->position, this->color);
|
||||||
}
|
}
|
||||||
|
|
||||||
void Ghost::collision(Entity* with) {
|
void Ghost::collision(Entity* with) {
|
||||||
|
Pacman* pacman = dynamic_cast<Pacman*>(with);
|
||||||
|
if(pacman != nullptr)
|
||||||
|
{
|
||||||
|
if(get_world().get_killmode()){
|
||||||
|
respawn_timer=respawn_time;
|
||||||
|
position=start_position;
|
||||||
|
} else{
|
||||||
|
pacman->queue_free();
|
||||||
|
get_world().lose();
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -3,13 +3,13 @@
|
||||||
#include "world.h"
|
#include "world.h"
|
||||||
|
|
||||||
int main() {
|
int main() {
|
||||||
create_world_with(1./5.);
|
|
||||||
|
|
||||||
World& world = get_world();
|
|
||||||
Vector2i window_size = get_map_size("assets/map");
|
Vector2i window_size = get_map_size("assets/map");
|
||||||
|
|
||||||
InitWindow(window_size.x, window_size.y, "Zoomba");
|
InitWindow(window_size.x, window_size.y, "Zoomba");
|
||||||
|
|
||||||
|
create_world_with(1./5.);
|
||||||
|
|
||||||
|
World& world = get_world();
|
||||||
world.load_atlas();
|
world.load_atlas();
|
||||||
world.set_size(window_size);
|
world.set_size(window_size);
|
||||||
load_world(world,"assets/map");
|
load_world(world,"assets/map");
|
||||||
|
|
|
||||||
|
|
@ -71,6 +71,14 @@ void load_world(World& world,const char* path) {
|
||||||
}
|
}
|
||||||
x++;
|
x++;
|
||||||
|
|
||||||
|
break;
|
||||||
|
case 'C':
|
||||||
|
{
|
||||||
|
Cherry* cherry = new Cherry;
|
||||||
|
cherry->position = {(float)x*16,(float)y*16};
|
||||||
|
world.entities.push_back(cherry);
|
||||||
|
}
|
||||||
|
x++;
|
||||||
break;
|
break;
|
||||||
default:
|
default:
|
||||||
x++;
|
x++;
|
||||||
|
|
|
||||||
|
|
@ -94,10 +94,11 @@ void Pacman::collision(Entity* with){
|
||||||
score->queue_free();
|
score->queue_free();
|
||||||
get_world().points+=10;
|
get_world().points+=10;
|
||||||
}
|
}
|
||||||
Ghost* ghost = dynamic_cast<Ghost*>(with);
|
Cherry* cherry = dynamic_cast<Cherry*>(with);
|
||||||
if (ghost != nullptr){
|
if (cherry != nullptr) {
|
||||||
queue_free();
|
cherry->queue_free();
|
||||||
get_world().lose();
|
get_world().points+=30;
|
||||||
|
get_world().start_killmode();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -9,3 +9,11 @@ Scorepoint::Scorepoint(){
|
||||||
void Scorepoint::draw() const {
|
void Scorepoint::draw() const {
|
||||||
DrawTextureRec(this->texture.get_texture(), this->texture.full_view(), this->position, WHITE);
|
DrawTextureRec(this->texture.get_texture(), this->texture.full_view(), this->position, WHITE);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Cherry::Cherry(){
|
||||||
|
texture = {get_world().get_atlas(),112,16,16,16};
|
||||||
|
}
|
||||||
|
|
||||||
|
void Cherry::draw() const {
|
||||||
|
DrawTextureRec(texture.get_texture(),texture.full_view(),position,WHITE);
|
||||||
|
}
|
||||||
|
|
|
||||||
|
|
@ -10,7 +10,6 @@ void init_world() {
|
||||||
World& world = get_world();
|
World& world = get_world();
|
||||||
Vector2i window_size = get_map_size("assets/map");
|
Vector2i window_size = get_map_size("assets/map");
|
||||||
|
|
||||||
world.load_atlas();
|
|
||||||
world.set_size(window_size);
|
world.set_size(window_size);
|
||||||
load_world(world,"assets/map");
|
load_world(world,"assets/map");
|
||||||
|
|
||||||
|
|
@ -21,6 +20,7 @@ World::World(){
|
||||||
this->entities = {};
|
this->entities = {};
|
||||||
this->grid = new Entity*[get_capacity()];
|
this->grid = new Entity*[get_capacity()];
|
||||||
this->seconds_per_tick = 0;
|
this->seconds_per_tick = 0;
|
||||||
|
|
||||||
this->clock = 0;
|
this->clock = 0;
|
||||||
this->debug = false;
|
this->debug = false;
|
||||||
this->bound_offset = 1;
|
this->bound_offset = 1;
|
||||||
|
|
@ -30,7 +30,7 @@ World::~World(){
|
||||||
for(int i = 0; i < this->entities.size();i++) {
|
for(int i = 0; i < this->entities.size();i++) {
|
||||||
delete this->entities[i];
|
delete this->entities[i];
|
||||||
}
|
}
|
||||||
UnloadTexture(this->texture_atlas);
|
UnloadTexture(texture_atlas);
|
||||||
}
|
}
|
||||||
|
|
||||||
void World::load_atlas() {
|
void World::load_atlas() {
|
||||||
|
|
@ -42,6 +42,14 @@ void World::setup(){
|
||||||
this->entities[i]->ready();
|
this->entities[i]->ready();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
bool World::get_killmode(){
|
||||||
|
return pacman_killmod_timer > 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
void World::start_killmode(){
|
||||||
|
pacman_killmod_timer = PACMAN_KILLTIME;
|
||||||
|
}
|
||||||
|
|
||||||
void World::process(){
|
void World::process(){
|
||||||
this->clock += GetFrameTime();
|
this->clock += GetFrameTime();
|
||||||
|
|
||||||
|
|
@ -76,6 +84,8 @@ void World::process(){
|
||||||
|
|
||||||
if (this->clock>=this->seconds_per_tick) {
|
if (this->clock>=this->seconds_per_tick) {
|
||||||
this->clock = 0;
|
this->clock = 0;
|
||||||
|
if(pacman_killmod_timer>0)
|
||||||
|
pacman_killmod_timer-=1;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Check for freed. Very very bad loop
|
// Check for freed. Very very bad loop
|
||||||
|
|
|
||||||
|
|
@ -20,6 +20,7 @@ enum LevelState{
|
||||||
Lost = 2,
|
Lost = 2,
|
||||||
};
|
};
|
||||||
|
|
||||||
|
const int PACMAN_KILLTIME = 45;
|
||||||
/// Class that holds information about game world
|
/// Class that holds information about game world
|
||||||
class World {
|
class World {
|
||||||
public:
|
public:
|
||||||
|
|
@ -45,6 +46,8 @@ class World {
|
||||||
int get_capacity() const;
|
int get_capacity() const;
|
||||||
int get_columns() const;
|
int get_columns() const;
|
||||||
int get_rows() const;
|
int get_rows() const;
|
||||||
|
void start_killmode();
|
||||||
|
bool get_killmode();
|
||||||
void load_atlas();
|
void load_atlas();
|
||||||
void lose();
|
void lose();
|
||||||
void win();
|
void win();
|
||||||
|
|
@ -53,6 +56,7 @@ class World {
|
||||||
int points = 0;
|
int points = 0;
|
||||||
private:
|
private:
|
||||||
float clock = 0.;
|
float clock = 0.;
|
||||||
|
int pacman_killmod_timer = 0;
|
||||||
int state = 0;
|
int state = 0;
|
||||||
void update_grid();
|
void update_grid();
|
||||||
Texture2D texture_atlas;
|
Texture2D texture_atlas;
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue