Skip to content

Commit

Permalink
Add ship sprite and tweak bullet parameters
Browse files Browse the repository at this point in the history
  • Loading branch information
zyperpl committed Dec 7, 2023
1 parent 48cbffa commit 847595d
Show file tree
Hide file tree
Showing 16 changed files with 57 additions and 90 deletions.
Binary file removed resources/craft_speederC.glb
Binary file not shown.
Binary file removed resources/robot.glb
Binary file not shown.
Binary file added resources/ship.aseprite
Binary file not shown.
Binary file removed resources/ship.glb
Binary file not shown.
Binary file removed resources/test.aseprite
Binary file not shown.
Binary file removed screenrec001.gif
Binary file not shown.
11 changes: 4 additions & 7 deletions src/asteroid.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,15 @@
#include "game.hpp"
#include "utils.hpp"

static constexpr const float ASTEROIDS_MIN_SIZE = 5.0f;
static constexpr const float ASTEROIDS_MIN_SIZE = 10.0f;

[[nodiscard]] Asteroid Asteroid::create(const Vector2 &position, float rotation, float size)
{
const float random_angle = GetRandomValue(0, 360) * DEG2RAD;
Asteroid asteroid;
asteroid.position = position;
asteroid.velocity.x = cos(random_angle + rotation * DEG2RAD + M_PI / 2.0f + M_PI) * 1.0f;
asteroid.velocity.y = sin(random_angle + rotation * DEG2RAD + M_PI / 2.0f + M_PI) * 1.0f;
asteroid.velocity.x = cos(random_angle + rotation * DEG2RAD + M_PI / 2.0f + M_PI) * 1.2f;
asteroid.velocity.y = sin(random_angle + rotation * DEG2RAD + M_PI / 2.0f + M_PI) * 1.2f;
asteroid.rotation_speed = GetRandomValue(-1, 1) * 0.01f;
asteroid.size = size;
return asteroid;
Expand All @@ -23,7 +23,7 @@ ObjectState Asteroid::update()
position.x += velocity.x;
position.y += velocity.y;

//wrap_position(position);
wrap_position(position);

rotation += rotation_speed;

Expand Down Expand Up @@ -57,7 +57,4 @@ void Asteroid::draw() const noexcept
DrawCircleLines(position.x - Game::width, position.y, size, RED);
DrawCircleLines(position.x, position.y + Game::height, size, RED);
DrawCircleLines(position.x, position.y - Game::height, size, RED);

const float s = 0.1f;
DrawSphereEx(Vector3{ position.x * s, 0.0f, position.y * s }, size * 0.2f, 4, 4, WHITE);
}
5 changes: 1 addition & 4 deletions src/bullet.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ ObjectState Bullet::update()
position.x += velocity.x;
position.y += velocity.y;

//wrap_position(position);
wrap_position(position);

life--;

Expand All @@ -18,7 +18,4 @@ ObjectState Bullet::update()
void Bullet::draw() const noexcept
{
DrawCircle(position.x, position.y, 2.0f, PINK);

const float s = 0.1f;
DrawSphereEx(Vector3{ position.x * s, 0.0f, position.y * s }, 0.2f, 4, 4, WHITE);
}
2 changes: 1 addition & 1 deletion src/bullet.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ class Bullet
public:
Vector2 position{};
Vector2 velocity{};
uint8_t life{ 40 };
uint8_t life{ 30 };

ObjectState update();
void draw() const noexcept;
Expand Down
5 changes: 0 additions & 5 deletions src/external/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,6 @@ SET(SUPPORT_FILEFORMAT_VOX OFF CACHE BOOL "" FORCE)
SET(SUPPORT_FILEFORMAT_XM OFF CACHE BOOL "" FORCE)
SET(SUPPORT_FILEFORMAT_MOD OFF CACHE BOOL "" FORCE)

SET(SUPPORT_MODULE_RMODELS ON CACHE BOOL "" FORCE)
SET(SUPPORT_FILEFORMAT_OBJ ON CACHE BOOL "" FORCE)
SET(SUPPORT_FILEFORMAT_MTL ON CACHE BOOL "" FORCE)
SET(SUPPORT_FILEFORMAT_GLTF ON CACHE BOOL "" FORCE)

SET(GLFW_USE_HYBRID_HPG OFF CACHE BOOL "" FORCE)
SET(GLFW_BUILD_EXAMPLES OFF CACHE BOOL "" FORCE)
SET(GLFW_BUILD_TESTS OFF CACHE BOOL "" FORCE)
Expand Down
25 changes: 3 additions & 22 deletions src/game.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,37 +29,18 @@ void Game::init()
asteroid.velocity.y = static_cast<float>(GetRandomValue(-1, 1)) * 0.5f;
asteroids->push(asteroid);
}

camera.position = Vector3{ static_cast<float>(width) / 2.0f, static_cast<float>(height) / 2.0f, 10.0f };
camera.target = Vector3{ static_cast<float>(width) / 2.0f, static_cast<float>(height) / 2.0f, 0.0f };
camera.up = Vector3{ 0.0f, 0.0f, 1.0f };
camera.fovy = 60.0f;
camera.projection = CAMERA_PERSPECTIVE;
}

void Game::update()
{
player->update();
bullets->update();
asteroids->update();

camera.position = Vector3 { 0.0f, 20.0f, 0.0f };
camera.target = Vector3{ player->position.x * 0.1f, player->position.y * 0.1f, 0.0f };
}

void Game::draw() noexcept
{
BeginMode3D(camera);
{
DrawCube(Vector3{ 0.0f, 0.0f, 0.0f }, 0.2f, 0.2f, 0.2f, BLUE);

player->draw();
bullets->draw();
asteroids->draw();

DrawGrid(100, 10.0f);
DrawSphere(Vector3{ 0.0f, 0.0f, 0.0f }, 1.0f, RED);

}
EndMode3D();
player->draw();
bullets->draw();
asteroids->draw();
}
9 changes: 3 additions & 6 deletions src/game.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,17 +21,14 @@ class Game
std::unique_ptr<ObjectCircularBuffer<Bullet>> bullets;
std::unique_ptr<ObjectCircularBuffer<Asteroid>> asteroids;

static constexpr int width = 960;
static constexpr int height = 540;
static constexpr int width = 640;
static constexpr int height = 360;
static constexpr float delta_time = 1.0f / 60.0f;
static constexpr int NUMBER_OF_ASTEROIDS = 10;
static constexpr int NUMBER_OF_ASTEROIDS = 30;

void init();

void update();

void draw() noexcept;

private:
Camera3D camera { };
};
6 changes: 3 additions & 3 deletions src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,7 @@ const bool integer_scaling = false;
void update_draw_frame()
{
Game &game = Game::get();
static RenderPass game_render_pass(Game::width * 2, Game::height * 2);
SetTextureFilter(game_render_pass.render_texture.texture, TEXTURE_FILTER_BILINEAR);
static RenderPass game_render_pass(Game::width, Game::height);
if (!game_render_pass.render_func)
game_render_pass.render_func = [&]()
{
Expand Down Expand Up @@ -60,7 +59,8 @@ void update_draw_frame()
const float screen_width_float = static_cast<float>(GetScreenWidth());
const float screen_height_float = static_cast<float>(GetScreenHeight());

float scale = std::min(screen_width_float / (float)(Game::width), screen_height_float / (float)(Game::height));
float scale =
std::max(std::min(screen_width_float / (float)(Game::width), screen_height_float / (float)(Game::height)), 1.0f);
if (integer_scaling)
scale = std::floor(scale);

Expand Down
66 changes: 33 additions & 33 deletions src/player.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,8 @@

void Player::draw() noexcept
{
sprite.rotation = rotation;

const int death_timer_int = static_cast<int>(death_timer.get_remaining_time() * 10.0f);
if (!death_timer.is_done() && death_timer_int % 2 == 0)
const int invincibility_timer_int = static_cast<int>(invincibility_timer.get_remaining_time() * 10.0f);
if (!invincibility_timer.is_done() && invincibility_timer_int % 2 == 0)
return;

sprite.origin = Vector2{ sprite.get_width() / 2.0f, sprite.get_height() / 2.0f };
Expand All @@ -35,52 +33,54 @@ void Player::draw() noexcept
sprite.position.y = position.y;
sprite.draw();

const float s = 0.1f;
DrawModelEx(model, Vector3{ position.x * s, 0.0f, position.y * s }, Vector3{ 0.0f, 1.0f, 0.0f }, -rotation, Vector3{ 1.0f, 1.0f, 1.0f }, WHITE);
DrawCircle(sprite.position.x, sprite.position.y, 2.0f, RED);
}

void Player::die()
{return;
{
lives--;
position.x = Game::width / 2.0f;
position.y = Game::height / 2.0f;
velocity.x = 0.0f;
velocity.y = 0.0f;

death_timer.start();
invincibility_timer.start();
}

void Player::handle_input()
{
if (IsKeyDown(KEY_SPACE))
sprite.set_tag("walk");
else
sprite.set_tag("idle");

if (IsKeyDown(KEY_LEFT))
rotation -= 1.0f * rotation_speed;
sprite.rotation -= 1.0f * rotation_speed;

if (IsKeyDown(KEY_RIGHT))
rotation += 1.0f * rotation_speed;
sprite.rotation += 1.0f * rotation_speed;

const bool is_accelerating = IsKeyDown(KEY_UP);

if (IsKeyDown(KEY_UP))
if (is_accelerating)
{
velocity.x -= cos(rotation * DEG2RAD + M_PI / 2.0f) * acceleration_speed;
velocity.y -= sin(rotation * DEG2RAD + M_PI / 2.0f) * acceleration_speed;
velocity.x -= cos(sprite.rotation * DEG2RAD + M_PI / 2.0f) * acceleration_speed;
velocity.y -= sin(sprite.rotation * DEG2RAD + M_PI / 2.0f) * acceleration_speed;

sprite.set_tag("fly");
}
else
{
sprite.set_tag("idle");
}

if (IsKeyDown(KEY_DOWN))
{
velocity.x += cos(rotation * DEG2RAD + M_PI / 2.0f) * acceleration_speed * 0.5f;
velocity.y += sin(rotation * DEG2RAD + M_PI / 2.0f) * acceleration_speed * 0.5f;
velocity.x += cos(sprite.rotation * DEG2RAD + M_PI / 2.0f) * acceleration_speed * 0.005f;
velocity.y += sin(sprite.rotation * DEG2RAD + M_PI / 2.0f) * acceleration_speed * 0.005f;
}

if (IsKeyDown(KEY_SPACE) && shoot_timer.is_done())
if (IsKeyDown(KEY_SPACE) && shoot_timer.is_done() && !is_invincible())
{
Bullet bullet;
bullet.position = position;
bullet.velocity.x = cos(rotation * DEG2RAD + M_PI / 2.0f + M_PI) * 5.0f;
bullet.velocity.y = sin(rotation * DEG2RAD + M_PI / 2.0f + M_PI) * 5.0f;
bullet.velocity.x = cos(sprite.rotation * DEG2RAD + M_PI / 2.0f + M_PI) * 5.0f;
bullet.velocity.y = sin(sprite.rotation * DEG2RAD + M_PI / 2.0f + M_PI) * 5.0f;
Game::get().bullets->push(bullet);

shoot_timer.start();
Expand All @@ -89,10 +89,7 @@ void Player::handle_input()

void Player::update()
{
death_timer.update(Game::delta_time);

if (!death_timer.is_done())
return;
invincibility_timer.update(Game::delta_time);

shoot_timer.update(Game::delta_time);
handle_input();
Expand All @@ -110,15 +107,18 @@ void Player::update()
velocity.y *= max_velocity;
}

//wrap_position(position);
wrap_position(position);

for (size_t i = Game::get().asteroids->tail; i < Game::get().asteroids->head; i++)
if (!is_invincible())
{
Asteroid &asteroid = Game::get().asteroids->objects[i];
if (CheckCollisionCircles(position, 2.0f, asteroid.position, asteroid.size))
for (size_t i = Game::get().asteroids->tail; i < Game::get().asteroids->head; i++)
{
die();
break;
Asteroid &asteroid = Game::get().asteroids->objects[i];
if (CheckCollisionCircles(position, 2.0f, asteroid.position, asteroid.size))
{
die();
break;
}
}
}

Expand Down
10 changes: 5 additions & 5 deletions src/player.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,9 @@
class Player
{
public:
Sprite sprite{ "resources/test.aseprite", "idle" };
Model model{ LoadModel("resources/ship.glb") };
Vector2 position{ 0.0f, 0.0f };
Sprite sprite{ "resources/ship.aseprite", "idle" };
Vector2 position{ Game::width / 2.0f, Game::height / 2.0f };
Vector2 velocity{ 0.0f, 0.0f };
float rotation{ 0.0f };

float rotation_speed{ 3.0f };
float acceleration_speed{ 0.04f };
Expand All @@ -26,12 +24,14 @@ class Player
int lives{ 3 };

Timer shoot_timer{ FRAMES(20) };
Timer death_timer{ FRAMES(120) };
Timer invincibility_timer{ FRAMES(180) };

void handle_input();

void update();

void draw() noexcept;
void die();

[[nodiscard]] bool is_invincible() const noexcept { return !invincibility_timer.is_done(); }
};
8 changes: 4 additions & 4 deletions src/utils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,14 +24,14 @@ Rectangle texture_rect_flipped(const Texture2D &texture)
void wrap_position(Vector2 &position)
{
if (position.x < 0)
position.x = Game::width * 10 - 1;
position.x = Game::width - 1;

if (position.x >= Game::width * 10)
if (position.x >= Game::width)
position.x = 0;

if (position.y < 0)
position.y = Game::height * 10 - 1;
position.y = Game::height - 1;

if (position.y >= Game::height * 10)
if (position.y >= Game::height)
position.y = 0;
}

0 comments on commit 847595d

Please sign in to comment.