feat: added spawner and player/truck are now rigidbody

main
Sara 2025-01-27 00:42:16 +01:00
parent 8b233db1b6
commit 4f6d68c503
6 changed files with 82 additions and 46 deletions

View File

@ -1,4 +1,5 @@
#include "player.hpp"
#include "truck.hpp"
#include "core/callable.hpp"
#include "core/canvas_engine.hpp"
#include "core/collidable_node.hpp"
@ -7,15 +8,14 @@
#include "core/input/input_map.hpp"
#include "core/math/transform.hpp"
#include "core/sprite.hpp"
#include "truck.hpp"
#include <SDL2/SDL_keyboard.h>
#include <SDL2/SDL_keycode.h>
#include <cmath>
Player::Player()
: ce::CollidableNode("player", 0x1u, 0x1u)
, sprite{this->create_child<ce::Sprite>("bike", "bike")}
, shape{this->create_child<ce::CollisionShape>("player_col_shape", ce::Shape::make_box(.4f, 0.75f))} {
: ce::RigidBody("player", 1.f, 0x1u, 0x1u)
, sprite{this->create_child<ce::Sprite>("bike", "bike")} {
this->create_child<ce::CollisionShape>("player_col_shape", 0.4, 0.1f);
this->set_global_transform(this->get_global_transform()
.translated({0.f, 2.f})
);
@ -36,7 +36,7 @@ Player::Player()
new ce::KeyboardScancode(SDL_SCANCODE_W),
new ce::KeyboardScancode(SDL_SCANCODE_S)
)
}) .changed.connect(ce::Callable<void, ce::InputValue>::make(
}).changed.connect(ce::Callable<void, ce::InputValue>::make(
this, &Player::_input_vertical_movement)
);
this->overlap_enter.connect(ce::Callable<void, ce::CollisionShape *, ce::CollidableNode *, ce::CollisionShape *>::make(
@ -44,17 +44,13 @@ Player::Player()
));
}
void Player::_tick(double const &delta) {
this->velocity = ce::Vecf::move_towards(
this->velocity,
this->input.scaled(SPEED),
ACCELERATION * delta
);
ce::Transform trans{this->get_global_transform().translated(this->velocity * delta)};
void Player::_tick(double delta) {
this->RigidBody::_tick(delta);
ce::Transform trans{this->get_global_transform()};
trans.position.x = std::clamp(trans.position.x, -LIMITS.x, LIMITS.x);
trans.position.y = std::clamp(trans.position.y, -LIMITS.y, LIMITS.y);
this->set_global_transform(trans);
this->add_acceleration(input * ACCELERATION);
if(this->invincibility > 0.f) {
this->invincibility -= delta;
@ -73,11 +69,9 @@ void Player::_input_vertical_movement(ce::InputValue value) {
this->input.y = value.get<float>().value_or(0.f);
}
void Player::_on_overlap_enter(ce::CollisionShape *, ce::CollidableNode *other, ce::CollisionShape *) {
void Player::_on_overlap_enter(ce::CollisionShape *, ce::CollidableNode *other, ce::CollisionShape *shape) {
if(this->invincibility > 0.f)
return;
if(Truck *truck{dynamic_cast<Truck*>(other)}) {
if(Truck *truck{dynamic_cast<Truck*>(other)})
this->invincibility = 2.f;
this->velocity = (this->get_global_transform().position - other->get_global_transform().position).normalized() * DAMAGE_FORCE;
}
}

View File

@ -1,24 +1,22 @@
#ifndef PLAYER_HPP
#define PLAYER_HPP
#include "core/collidable_node.hpp"
#include "core/input/input_value.hpp"
#include "core/rigidbody.hpp"
namespace ce {
class CollisionShape;
class Sprite;
};
class Player : public ce::CollidableNode {
class Player : public ce::RigidBody {
ce::Vecf const SPEED{3.f, 2.5f};
float const ACCELERATION{10.f};
ce::Vecf const LIMITS{3.f, 3.f};
ce::Vecf const LIMITS{2.5f, 3.f};
float const DAMAGE_FLASH_FREQ{.1f};
float const DAMAGE_FORCE{6.f};
ce::Vecf velocity{0.f, 0.f};
ce::Vecf input{0.f, 0.f};
ce::CollisionShape *shape{nullptr};
ce::Sprite *sprite{nullptr};
float invincibility{0.f};
public:
@ -26,7 +24,7 @@ public:
virtual void _tick(double delta) override;
void _input_horizontal_movement(ce::InputValue value);
void _input_vertical_movement(ce::InputValue value);
void _on_overlap_enter(ce::CollisionShape *, ce::CollidableNode *node, ce::CollisionShape*);
void _on_overlap_enter(ce::CollisionShape *, ce::CollidableNode *node, ce::CollisionShape *shape);
};
#endif // !PLAYER_HPP

24
src/spawner.cpp Normal file
View File

@ -0,0 +1,24 @@
#include "spawner.hpp"
#include "truck.hpp"
#include <algorithm>
#include <cstdlib>
Spawner::Spawner()
: ce::Node("Spawner")
{}
void Spawner::_tick(double delta) {
this->timer -= delta;
if(this->timer < 0.0) {
this->timer = this->time_between_trucks;
this->time_between_trucks = std::max(
this->time_between_trucks - this->TIME_LESS_PER_TRUCK,
this->MIN_TIME_BETWEEN_TRUCKS
);
this->spawn_truck();
}
}
void Spawner::spawn_truck() {
Truck *truck{this->create_child<Truck>(std::rand() % 2 != 0)};
}

19
src/spawner.hpp Normal file
View File

@ -0,0 +1,19 @@
#ifndef SPAWNER_HPP
#define SPAWNER_HPP
#include "core/node.hpp"
class Spawner : public ce::Node {
private:
double const MIN_TIME_BETWEEN_TRUCKS{2.f};
double const TIME_LESS_PER_TRUCK{0.25};
double time_between_trucks{7.0};
double timer{0.0};
public:
Spawner();
virtual void _tick(double delta) override;
private:
void spawn_truck();
};
#endif // !SPAWNER_HPP

View File

@ -1,29 +1,34 @@
#include "truck.hpp"
#include "core/collision_shape.hpp"
#include "core/math/transform.hpp"
#include "core/sprite.hpp"
#include "core/collision_shape.hpp"
#include <algorithm>
#include <cmath>
#include <SDL2/SDL_log.h>
Truck::Truck(bool left)
: ce::CollidableNode("truck", 0x1u, 0x1u)
, sprite{this->create_child<ce::Sprite>("sprite", "truck")}
, shape{this->create_child<ce::CollisionShape>("truck_col_shape", ce::Shape::make_box(.5f, 1.f))}
, spawned_left{left} {
this->sprite->set_global_transform(this->get_global_transform() .scaled({2.5f, 2.5f}));
: ce::RigidBody("truck", 20.f, 0x1u, 0x1u)
, sprite{this->create_child<ce::Sprite>("truck_sprite", "truck")} {
this->create_child<ce::CollisionShape>("truck_col_shape_0", 0.6f, 0.1f)->set_transform(ce::Transform().translated({0.f, 0.5f}));
this->create_child<ce::CollisionShape>("truck_col_shape_1", 0.6f, 0.1f)->set_transform(ce::Transform().translated({0.f, -0.5f}));
this->sprite->set_global_transform(this->get_global_transform().scaled({2.5f, 2.5f}));
this->set_global_transform(this->get_global_transform().translated({
.x=this->spawned_left ? -1.4f : 1.4f,
.x=left ? -1.4f : 1.4f,
.y=-4.5f
}));
}
wave_time += delta * FREQUENCY;
ce::Transform const transform{this->get_global_transform()
.translated(ce::Vecf{
.x=std::sin(wave_time) * AMPLITUDE * (this->spawned_left > 0.f ? 1.f : -1.f),
.y=this->RELATIVE_VERTICAL_SPEED
} * float(delta))
};
this->set_global_transform(transform);
void Truck::_tick(double delta) {
this->ce::RigidBody::_tick(delta);
ce::Transform const transform{this->get_global_transform()};
this->add_acceleration({
(transform.position.x > 0.f ? -this->ACCELERATION.x : this->ACCELERATION.x),
this->ACCELERATION.y
});
ce::Transform trans{this->get_global_transform()};
trans.position.x = std::clamp(trans.position.x, -LIMITS.x, LIMITS.x);
trans.position.y = std::max(trans.position.y, -LIMITS.y);
this->set_global_transform(trans);
if(transform.position.y > 4.5f)
this->flag_for_deletion();
}

View File

@ -1,24 +1,20 @@
#ifndef TRUCK_HPP
#define TRUCK_HPP
#include "core/collidable_node.hpp"
#include "core/rigidbody.hpp"
namespace ce {
class Sprite;
class CollisionShape;
};
class Truck : public ce::CollidableNode {
float const AMPLITUDE{4.f};
float const FREQUENCY{3.f};
float const RELATIVE_VERTICAL_SPEED{1.5f};
float wave_time{0.f};
bool spawned_left{false};
class Truck : public ce::RigidBody {
ce::Sprite *sprite{nullptr};
ce::CollisionShape *shape{nullptr};
ce::Vecf const LIMITS{2.5f, 5.f};
ce::Vecf const ACCELERATION{5.f, 25.f};
public:
Truck(bool left);
virtual void _tick(double const &delta) override;
virtual void _tick(double delta) override;
};
#endif // !TRUCK_HPP