Skip to content

Commit

Permalink
Separate PlayerShip from Player
Browse files Browse the repository at this point in the history
  • Loading branch information
zyperpl committed Dec 17, 2023
1 parent 48aa923 commit b209c78
Show file tree
Hide file tree
Showing 13 changed files with 313 additions and 255 deletions.
1 change: 1 addition & 0 deletions src/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ ADD_EXECUTABLE(${PROJECT_NAME}
particle.cpp
pickable.cpp
player.cpp
player_ship.cpp
render_pass.cpp
resource.cpp
sound_manager.cpp
Expand Down
18 changes: 9 additions & 9 deletions src/asteroid.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ bool Asteroid::update()

wrap_position(position);

Game::get().bullets->for_each(
GAME.bullets->for_each(
[&](Bullet &bullet) -> bool
{
if (bullet.life <= 0)
Expand All @@ -66,7 +66,7 @@ bool Asteroid::update()
die();

for (int i = 0; i < 20; i++)
Game::get().particles->push(create_asteroid_particle(position, 100));
GAME.particles->push(create_asteroid_particle(position, 100));

bullet.life = 0;
return false;
Expand All @@ -89,13 +89,13 @@ void Asteroid::die()
{
for (size_t i = 0; i < ASTEROID_SPLIT_COUNT; i++)
{
Game::get().asteroids->push(Asteroid::create(position, size - 1));
GAME.asteroids->push(Asteroid::create(position, size - 1));
}
}

for (int i = 0; i < 20 - std::max(1, size * 5); i++)
{
Game::get().particles->push(create_asteroid_particle(position));
GAME.particles->push(create_asteroid_particle(position));
}

int r = GetRandomValue(0, 100);
Expand All @@ -114,11 +114,11 @@ void Asteroid::die()
{
const Vector2 pos{ position.x + static_cast<float>(GetRandomValue(-4 * size, 4 * size)),
position.y + static_cast<float>(GetRandomValue(-3 * size, 3 * size)) };
Game::get().pickables->push(Pickable::create_ore(pos, Vector2Scale(velocity, 0.5f)));
GAME.pickables->push(Pickable::create_ore(pos, Vector2Scale(velocity, 0.5f)));
}
}

Game::get().score += 100 * (3 - size);
GAME.score += 100 * (3 - size);
}

void Asteroid::draw() const noexcept
Expand All @@ -132,19 +132,19 @@ void Asteroid::draw() const noexcept
sprite.position = P;
sprite.draw();

if (Game::CONFIG.show_masks)
if (CONFIG(show_masks))
{
Mask mask_copy = mask;
mask_copy.position = P;
mask_copy.draw();
}
});

if (Game::CONFIG.show_debug)
if (CONFIG(show_debug))
{
DrawPixelV(position, PINK);
DrawText(TextFormat("%d", size), position.x, position.y, 10, RED);
}
if (Game::CONFIG.show_velocity)
if (CONFIG(show_velocity))
DrawLineEx(position, Vector2{ position.x + velocity.x * 20.0f, position.y + velocity.y * 20.0f }, 1.0f, RED);
}
8 changes: 4 additions & 4 deletions src/bullet.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ static Vector2 DEBUG_asteroid_position;

const Asteroid &get_nearest_asteroid(const Vector2 &position)
{
auto &asteroids = Game::get().asteroids;
auto &asteroids = GAME.asteroids;
const Asteroid *nearest_asteroid = &asteroids->objects[0];
float nearest_distance{ std::numeric_limits<float>::max() };

Expand Down Expand Up @@ -79,7 +79,7 @@ bool Bullet::update()
for (size_t i = 0; i < number_of_particles; ++i)
{
const Vector2 velocity{ GetRandomValue(-100, 100) / 100.0f, GetRandomValue(-100, 100) / 100.0f };
Game::get().particles->push(Particle::create(position, velocity, particle_color));
GAME.particles->push(Particle::create(position, velocity, particle_color));
}
return false;
}
Expand Down Expand Up @@ -113,7 +113,7 @@ bool Bullet::update()
particles_per_frame = 1;
if (life % particles_per_frame == 0)
{
Game::get().particles->push(Particle::create(position, Vector2{ 0.0f, 0.0f }, particle_color));
GAME.particles->push(Particle::create(position, Vector2{ 0.0f, 0.0f }, particle_color));
}

return true;
Expand All @@ -130,7 +130,7 @@ void Bullet::draw() const noexcept
[&](const Vector2 &P) { DrawCircle(P.x, P.y, 2.0f, color); });

#if defined(DEBUG)
if (Game::get().CONFIG.debug_bullets)
if (CONFIG(debug_bullets))
{
DrawCircleV(DEBUG_asteroid_position, 2.0f, RED);
DrawCircleLinesV(DEBUG_asteroid_position, 20.0f, RED);
Expand Down
6 changes: 3 additions & 3 deletions src/game.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,10 @@
#include "object_circular_buffer.hpp"
#include "particle.hpp"
#include "pickable.hpp"
#include "player.hpp"
#include "player_ship.hpp"
#include "utils.hpp"

Config Game::CONFIG{};
Config Game::config{};
uint64_t Game::frame{ 0 };

Game &Game::get() noexcept
Expand All @@ -22,7 +22,7 @@ Game &Game::get() noexcept

void Game::init()
{
player = std::make_unique<Player>();
player = std::make_unique<PlayerShip>();
bullets = std::make_unique<ObjectCircularBuffer<Bullet, 128>>();
asteroids = std::make_unique<ObjectCircularBuffer<Asteroid, 1024>>();
particles = std::make_unique<ObjectCircularBuffer<Particle, 4096>>();
Expand Down
5 changes: 4 additions & 1 deletion src/game.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@
#include <raylib.h>
#include <raymath.h>

#define CONFIG(Option) Game::config.Option
#define GAME Game::get()

class Player;
class Bullet;
class Asteroid;
Expand Down Expand Up @@ -38,7 +41,7 @@ class Game
static constexpr int height = 360;
static constexpr float delta_time = 1.0f / 60.0f;
static constexpr int NUMBER_OF_ASTEROIDS = 10;
static Config CONFIG;
static Config config;
static uint64_t frame;

void init();
Expand Down
4 changes: 2 additions & 2 deletions src/particle.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ bool Particle::update() noexcept

wrap_position(position);

Game::get().asteroids->for_each(
GAME.asteroids->for_each(
[&](Asteroid &asteroid)
{
const Vector2 &asteroid_position = asteroid.position;
Expand All @@ -44,7 +44,7 @@ bool Particle::update() noexcept
return true;
});

const auto &player = Game::get().player;
const auto &player = GAME.player;
const Vector2 &player_position = player->position;
const float distance = Vector2Distance(position, player_position);

Expand Down
23 changes: 13 additions & 10 deletions src/pickable.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@ Pickable Pickable::create_ore(const Vector2 &position, const Vector2 &velocity)
velocity,
[]()
{
Game::get().coins += 1;
Game::get().score += 100;
GAME.coins += 1;
GAME.score += 100;
});
}

Expand All @@ -40,14 +40,14 @@ bool Pickable::update()
mask.position = position;
wrap_position(position);

if (!Game::get().player)
if (!GAME.player)
return true;

if (!player)
{
if (mask.check_collision(Game::get().player->mask))
if (mask.check_collision(GAME.player->get_mask()))
{
player = Game::get().player.get();
player = GAME.player.get();

velocity = Vector2Normalize(Vector2Subtract(position, player->position));
velocity.x *= 1.1f;
Expand All @@ -68,14 +68,17 @@ bool Pickable::update()
}
const Vector2 dir = Vector2Normalize(Vector2Subtract(player->position, position));

if (d > 64.0f)
if (d > std::min(Game::width, Game::height) / 2.0f)
{
position.x = player->position.x - dir.x * 16.0f;
position.y = player->position.y - dir.y * 16.0f;
position.x = player->position.x - dir.x * 7.0f;
position.y = player->position.y - dir.y * 7.0f;
}

velocity.x += dir.x * 0.3f;
velocity.y += dir.y * 0.3f;
velocity.x += dir.x * 0.1f;
velocity.y += dir.y * 0.1f;

position.x += dir.x * 0.4f;
position.y += dir.y * 0.4f;
}

return true;
Expand Down
Loading

0 comments on commit b209c78

Please sign in to comment.