Skip to content

Commit

Permalink
Add pickable crystal ore
Browse files Browse the repository at this point in the history
  • Loading branch information
zyperpl committed Dec 14, 2023
1 parent 4e543bd commit beeac24
Show file tree
Hide file tree
Showing 10 changed files with 126 additions and 66 deletions.
Binary file added resources/ore.aseprite
Binary file not shown.
23 changes: 23 additions & 0 deletions src/asteroid.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
#include "bullet.hpp"
#include "game.hpp"
#include "particle.hpp"
#include "pickable.hpp"
#include "utils.hpp"

static constexpr const float ASTEROIDS_SIZE[] = { 8.0f, 16.0f, 32.0f };
Expand Down Expand Up @@ -96,6 +97,28 @@ void Asteroid::die()
{
Game::get().particles->push(create_asteroid_particle(position));
}

int r = GetRandomValue(0, 100);
if (r > 60)
{
int pickables_n = size;
if (size >= 2)
{
r = GetRandomValue(0, 100);
if (r < 10)
pickables_n = 3;
else if (r == 10)
pickables_n = 4;
}
for (int i = 0; i < pickables_n; i++)
{
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::get().score += 100 * (3 - size);
}

void Asteroid::draw() const noexcept
Expand Down
2 changes: 1 addition & 1 deletion src/asteroid.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ class Asteroid
Vector2 position{};
Vector2 velocity{};
int size{ 2 };
int max_life { 2 };
int max_life { 1 };
int life{ max_life };
mutable Sprite sprite{ "resources/asteroid.aseprite" };
Mask mask{};
Expand Down
8 changes: 3 additions & 5 deletions src/game.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -52,9 +52,7 @@ void Game::init()
{
const Vector2 position = { static_cast<float>(GetRandomValue(0, width)),
static_cast<float>(GetRandomValue(0, height)) };
Pickable pickable;
pickable.position = position;
pickables->push(std::move(pickable));
pickables->push(Pickable::create_ore(position, Vector2Zero()));
}

for (size_t i = 0; i < stars.size(); i++)
Expand Down Expand Up @@ -104,9 +102,9 @@ void Game::draw_background() noexcept
{
const Vector2 &star = stars[i];
if (i % 2 == 0)
DrawPixel(star.x, star.y, Color{ 180, 180, 100, 255 });
DrawPixel(star.x, star.y, Color{ 240, 180, 100, 255 });
else
DrawPixel(star.x, star.y, Color{ 120, 120, 100, 255 });
DrawPixel(star.x, star.y, Color{ 120, 230, 100, 255 });
}

auto asteroid_sprite = Sprite{ "resources/asteroid.aseprite" };
Expand Down
7 changes: 5 additions & 2 deletions src/game.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,10 @@ class ObjectCircularBuffer;

struct Config
{
bool show_debug{ true };
bool show_debug{ false };
bool show_masks{ false };
bool show_velocity{ false };
bool debug_bullets { false };
bool debug_bullets{ false };
};

class Game
Expand All @@ -47,6 +47,9 @@ class Game

void draw() noexcept;

size_t coins{ 0 };
size_t score{ 0 };

private:
[[nodiscard]] Game() noexcept = default;

Expand Down
40 changes: 18 additions & 22 deletions src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -46,33 +46,29 @@ void update_draw_frame()
{
const float font_size = 10.0f;
DrawText(TextFormat("FPS: %i", GetFPS()), 10, 10, font_size, WHITE);
DrawText(TextFormat("Bullets: %i (%zu - %zu)", game.bullets->size(), game.bullets->tail, game.bullets->head),
10,
30,
font_size,
WHITE);
DrawText(
TextFormat("Asteroids: %i (%zu - %zu)", game.asteroids->size(), game.asteroids->tail, game.asteroids->head),
10,
50,
font_size,
WHITE);

DrawText("Lives: ", 10, 70, font_size, WHITE);
Vector2 text_size = MeasureTextEx(GetFontDefault(), "Lives: ", font_size, 1.0f);
const float x = 15 + text_size.x;
const float y = 70 + text_size.y * 0.5f;

const char *lives_text = "Lives: ";
DrawText(lives_text, 10, 70, font_size, WHITE);
Vector2 text_size = MeasureTextEx(GetFontDefault(), lives_text, font_size, 1.0f);
float x = 15 + text_size.x;
float y = 70 + text_size.y * 0.5f;
for (int i = 0; i < game.player->lives; i++)
{
DrawCircle(x + i * 20, y, 5, RED);
}

DrawText(
TextFormat("Particles: %i (%zu - %zu)", game.particles->size(), game.particles->tail, game.particles->head),
10,
90,
font_size,
WHITE);
DrawText(TextFormat("Score: %i", game.score), 10, 90, font_size, WHITE);

const char *crystals_text = TextFormat("Crystals: %i", game.coins);
DrawText(crystals_text, 10, 110, font_size, WHITE);
text_size = MeasureTextEx(GetFontDefault(), crystals_text, font_size, 1.0f);
static Sprite crystal_sprite{ "resources/ore.aseprite" };
crystal_sprite.set_frame(0);
crystal_sprite.scale = Vector2{ 0.4f, 0.4f };
crystal_sprite.set_centered();
crystal_sprite.position.x = 15 + text_size.x + 2;
crystal_sprite.position.y = 110 + text_size.y * 0.5f;
crystal_sprite.draw();
};

const float screen_width_float = static_cast<float>(GetScreenWidth());
Expand Down
89 changes: 60 additions & 29 deletions src/pickable.cpp
Original file line number Diff line number Diff line change
@@ -1,63 +1,94 @@
#include "pickable.hpp"

#include <functional>

#include "game.hpp"
#include "particle.hpp"
#include "player.hpp"
#include "utils.hpp"

Pickable::Pickable(Sprite &&sprite, const Vector2 &position, std::function<void()> func)
: sprite{ std::move(sprite) }
, position{ position }
Pickable Pickable::create(const Vector2 &position, const Vector2 &velocity, std::function<void()> func)
{
Pickable pickable(position, func);
pickable.velocity = velocity;
return pickable;
}

Pickable Pickable::create_ore(const Vector2 &position, const Vector2 &velocity)
{
return create(position,
velocity,
[]()
{
Game::get().coins += 1;
Game::get().score += 100;
});
}

Pickable::Pickable(const Vector2 &position, std::function<void()> func)
: position{ position }
, velocity{ 0.0f, 0.0f }
, func{ func }
{
sprite.set_frame(0);
sprite.set_centered();
}

bool Pickable::update()
{
position.x += velocity.x;
position.y += velocity.y;

mask.position = position;
wrap_position(position);

mask.position = position;
if (!Game::get().player)
return true;

if (Game::get().player)
if (!player)
{
const Player &player = *Game::get().player;
const Mask player_mask = player.mask;
if (mask.check_collision(Game::get().player->mask))
{
player = Game::get().player.get();

velocity = Vector2Normalize(Vector2Subtract(position, player->position));
velocity.x *= 1.1f;
velocity.y *= 1.1f;

if (mask.check_collision(player_mask))
sprite.scale = Vector2{ 0.86f, 0.86f };
}
}
else
{
const float d = Vector2Distance(position, player->position);
if (d < 8.0f)
{
if (func)
func();

return false;
}
const Vector2 dir = Vector2Normalize(Vector2Subtract(player->position, position));

if (d > 64.0f)
{
position.x = player->position.x - dir.x * 16.0f;
position.y = player->position.y - dir.y * 16.0f;
}

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

return true;
}

void Pickable::draw() const
{
if (IsTextureReady(sprite.get_texture()))
{
draw_wrapped(sprite.get_destination_rect(),
[&](const Vector2 &position)
{
sprite.position.x = position.x;
sprite.draw();
});
}

if (Game::get().CONFIG.show_debug)
{
draw_wrapped(Rectangle{ position.x, position.y, 10.0f, 10.0f },
[&](const Vector2 &position)
{
mask.position = position;
mask.draw();
});
}
sprite.set_centered();
sprite.position = position;
draw_wrapped(sprite.get_destination_rect(),
[&](const Vector2 &position)
{
sprite.position = position;
sprite.draw();
});
}
18 changes: 13 additions & 5 deletions src/pickable.hpp
Original file line number Diff line number Diff line change
@@ -1,23 +1,31 @@
#pragma once

#include <functional>

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

class Player;

class Pickable
{
public:
Pickable() {}
Pickable(Sprite &&sprite, const Vector2 &position, std::function<void()> func);

static Pickable create(const Vector2 &position, const Vector2 &velocity, std::function<void()> func);
static Pickable create_ore(const Vector2 &position, const Vector2 &velocity);
bool update();
void draw() const;

mutable Sprite sprite;
mutable Mask mask{ Circle{ Vector2{ 0.0f, 0.0f }, 10.0f } };
mutable Sprite sprite{ "resources/ore.aseprite" };
mutable Mask mask{ Circle{ Vector2{ 0.0f, 0.0f }, 24.0f } };
Vector2 position{ 0.0f, 0.0f };
Vector2 velocity{ 0.0f, 0.0f };
const Player *player{ nullptr };

private:
Pickable() = default;
Pickable(const Vector2 &position, std::function<void()> func);
std::function<void()> func;

DECLARE_FRIEND_OBJECT_CIRCULAR_BUFFER()
};
2 changes: 1 addition & 1 deletion src/player.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,7 @@ void Player::handle_input()
bullet_velocity.x += velocity.x * 0.5f;
bullet_velocity.y += velocity.y * 0.5f;

BulletType bullet_type = BulletType::Homing;
BulletType bullet_type = BulletType::Normal;

if (bullet_type == BulletType::Normal)
{
Expand Down
3 changes: 2 additions & 1 deletion src/sprite.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,8 @@ size_t Sprite::get_height() const

void Sprite::set_centered()
{
origin = Vector2{ static_cast<float>(get_width()) / 2.0f, static_cast<float>(get_height()) / 2.0f };
origin =
Vector2{ static_cast<float>(scale.x * get_width()) / 2.0f, static_cast<float>(scale.y * get_height()) / 2.0f };
}

Rectangle Sprite::get_source_rect() const
Expand Down

0 comments on commit beeac24

Please sign in to comment.