Skip to content

Commit

Permalink
Add interactable class and implement station
Browse files Browse the repository at this point in the history
  • Loading branch information
zyperpl committed Dec 25, 2023
1 parent 22cbb1b commit b129a70
Show file tree
Hide file tree
Showing 6 changed files with 95 additions and 42 deletions.
1 change: 1 addition & 0 deletions src/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ ADD_EXECUTABLE(${PROJECT_NAME}
asteroid.cpp
bullet.cpp
game.cpp
interactable.cpp
main.cpp
mask.cpp
particle.cpp
Expand Down
52 changes: 19 additions & 33 deletions src/game.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

#include "asteroid.hpp"
#include "bullet.hpp"
#include "interactable.hpp"
#include "object_circular_buffer.hpp"
#include "particle.hpp"
#include "pickable.hpp"
Expand Down Expand Up @@ -37,9 +38,11 @@ void Game::init()
TraceLog(LOG_TRACE, "Size of Pickable buffer: %zukB", sizeof(Pickable) * pickables->capacity / 1024);

for (size_t i = 0; i < stars.size(); i++)
{
stars[i] = Vector2{ static_cast<float>(GetRandomValue(0, width)), static_cast<float>(GetRandomValue(0, height)) };
}

interactables.reserve(128);

TraceLog(LOG_TRACE, "Game initialized");
}

void Game::update()
Expand All @@ -64,6 +67,9 @@ void Game::update()

void Game::update_game()
{
for (auto &interactable : interactables)
interactable->update();

if (state == GameState::PLAYING_ASTEROIDS)
{
if (!freeze_entities)
Expand All @@ -78,22 +84,6 @@ void Game::update_game()

update_background();

if (!station)
{
if (asteroids->empty())
{
station = std::make_unique<Sprite>("resources/station.aseprite");
station->position = Vector2{ width / 2.0f, height / 2.0f };
station->scale = Vector2{ 0.1f, 0.1f };
}
}
else
{
station->position = Vector2{ width / 2.0f, height / 2.0f + sin(frame * 0.001f) * 10.0f };
if (station->scale.x < 1.0f)
station->scale = Vector2Add(station->scale, Vector2{ 0.01f, 0.01f });
}

#if defined(DEBUG)

if (IsKeyDown(KEY_LEFT_SHIFT))
Expand All @@ -118,7 +108,7 @@ void Game::update_game()
}

if (state == GameState::PLAYING_STATION)
{
{
if (!freeze_entities)
{
player->update();
Expand Down Expand Up @@ -150,12 +140,8 @@ void Game::draw() noexcept
{
case GameState::PLAYING_ASTEROIDS:
{
if (station)
{
station->tint = ColorBrightness(BLACK, 0.7f);
station->set_centered();
station->draw();
}
for (const auto &interactable : interactables)
interactable->draw();

particles->for_each(std::bind(&Particle::draw, std::placeholders::_1));

Expand Down Expand Up @@ -260,6 +246,9 @@ void Game::set_state(GameState new_state) noexcept
static_cast<float>(GetRandomValue(0, height)) };
pickables->push(Pickable::create_ore(position, Vector2Zero()));
}

interactables.push_back(std::make_unique<Station>());

break;
case GameState::PLAYING_STATION:
player = std::make_unique<PlayerCharacter>();
Expand All @@ -281,7 +270,7 @@ void Game::play_action(const Action::Type &action_type, const Level &level) noex
Action action;
action.on_update = [this](Action &action)
{
const auto &station_position = station->position;
const auto &station_position = Vector2 { width * 0.5f, height * 0.5f };

PlayerShip *player_ship = dynamic_cast<PlayerShip *>(player.get());
if (!player_ship)
Expand Down Expand Up @@ -359,18 +348,15 @@ void Game::play_action(const Action::Type &action_type, const Level &level) noex

#if defined(ALPHA_FADEOUT)
float alpha = std::max(0.0f, std::min(255.0f, 255.0f * data));
alpha = std::floor(alpha / 20.0f) * 20.0f;
Color color { 0, 0, 0, static_cast<unsigned char>(alpha) };
alpha = std::floor(alpha / 20.0f) * 20.0f;
Color color{ 0, 0, 0, static_cast<unsigned char>(alpha) };
DrawRectangle(0, 0, width, height, color);
#endif

DrawRing(Vector2 { width * 0.5f, height * 0.5f }, width * data, width, 0.0f, 360.0f, 8, BLACK);
DrawRing(Vector2{ width * 0.5f, height * 0.5f }, width * data, width, 0.0f, 360.0f, 8, BLACK);
};

action.on_done = [this]()
{
freeze_entities = false;
};
action.on_done = [this]() { freeze_entities = false; };

action.data = 0.0f;
actions.push(std::move(action));
Expand Down
5 changes: 2 additions & 3 deletions src/game.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ class Bullet;
class Asteroid;
class Particle;
class Pickable;
class Interactable;

template<typename T, size_t>
struct ObjectCircularBuffer;
Expand Down Expand Up @@ -89,8 +90,7 @@ class Game
std::unique_ptr<ObjectCircularBuffer<Asteroid, 1024>> asteroids;
std::unique_ptr<ObjectCircularBuffer<Particle, 4096>> particles;
std::unique_ptr<ObjectCircularBuffer<Pickable, 1024>> pickables;

const auto *get_station() const noexcept { return station.get(); }
std::vector<std::unique_ptr<Interactable>> interactables;

static constexpr int width = 480;
static constexpr int height = 270;
Expand Down Expand Up @@ -118,7 +118,6 @@ class Game
void update_game();

std::array<Vector2, 100> stars;
std::unique_ptr<Sprite> station;
void update_background() noexcept;
void draw_background() noexcept;

Expand Down
35 changes: 35 additions & 0 deletions src/interactable.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
#include "interactable.hpp"

#include "asteroid.hpp"
#include "game.hpp"
#include "utils.hpp"

void Interactable::draw() const
{
sprite.set_centered();
sprite.draw();
}

Station::Station()
{
sprite = Sprite{ "resources/station.aseprite", "idle" };
sprite.set_centered();
sprite.position = Vector2{ Game::width * 0.5f, Game::height * 0.5f };
sprite.scale = Vector2{ 0.01f, 0.01f };
sprite.tint = ColorBrightness(BLACK, 0.7f);
}

void Station::update()
{
const auto state = GAME.get_state();
if (state != GameState::PLAYING_ASTEROIDS)
return;

if (!GAME.asteroids->empty())
return;

const auto &frame = GAME.frame;
sprite.position = Vector2{ GAME.width / 2.0f, GAME.height / 2.0f + sin(frame * 0.001f) * 10.0f };
if (sprite.scale.x < 1.0f)
sprite.scale = Vector2Add(sprite.scale, Vector2{ 0.01f, 0.01f });
}
32 changes: 32 additions & 0 deletions src/interactable.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
#pragma once

#include "mask.hpp"
#include "sprite.hpp"

class Interactable
{
public:
virtual ~Interactable() = default;

virtual void update() = 0;
virtual void draw() const;

const Sprite &get_sprite() const noexcept { return sprite; }
protected:
mutable Sprite sprite{};
};

class Station final : public Interactable
{
public:
Station();
void update() override;

private:
};

class DialogEntity final : public Interactable
{
public:
void update() override;
};
12 changes: 6 additions & 6 deletions src/player_ship.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
#include "asteroid.hpp"
#include "bullet.hpp"
#include "game.hpp"
#include "interactable.hpp"
#include "particle.hpp"
#include "utils.hpp"

Expand Down Expand Up @@ -133,7 +134,7 @@ bool PlayerShip::can_shoot() const noexcept

bool PlayerShip::can_interact() const noexcept
{
return interactive_found_timer.is_done() && !is_invincible() && lives > 0 && is_near_interactive();
return interactive_found_timer.is_done() && !is_invincible() && is_near_interactive();
}

void PlayerShip::handle_input()
Expand Down Expand Up @@ -254,15 +255,14 @@ void PlayerShip::calculate_nearest_interactive() noexcept
nearest_interactive.second.x = 0.0f;
nearest_interactive.second.y = 0.0f;

// station
if (auto *station = game.get_station(); station)
for (auto &obj : game.interactables)
{
const float min_distance = std::max(station->get_width(), station->get_height()) * 0.5f;
const float distance = Vector2Distance(position, station->position);
const float min_distance = std::max(obj->get_sprite().get_width(), obj->get_sprite().get_height()) * 0.5f;
const float distance = Vector2Distance(position, obj->get_sprite().position);
if (distance < min_distance)
{
nearest_interactive.first = InteractiveType::STATION;
nearest_interactive.second = station->position;
nearest_interactive.second = obj->get_sprite().position;

if (interactive_found_timer.is_done())
interactive_found_timer.start();
Expand Down

0 comments on commit b129a70

Please sign in to comment.