diff --git a/src/player.cpp b/src/player.cpp index dcc59b0..1b95a64 100644 --- a/src/player.cpp +++ b/src/player.cpp @@ -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 #include #include Player::Player() -: ce::CollidableNode("player", 0x1u, 0x1u) -, sprite{this->create_child("bike", "bike")} -, shape{this->create_child("player_col_shape", ce::Shape::make_box(.4f, 0.75f))} { +: ce::RigidBody("player", 1.f, 0x1u, 0x1u) +, sprite{this->create_child("bike", "bike")} { + this->create_child("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::make( + }).changed.connect(ce::Callable::make( this, &Player::_input_vertical_movement) ); this->overlap_enter.connect(ce::Callable::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().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(other)}) { + if(Truck *truck{dynamic_cast(other)}) this->invincibility = 2.f; - this->velocity = (this->get_global_transform().position - other->get_global_transform().position).normalized() * DAMAGE_FORCE; - } } diff --git a/src/player.hpp b/src/player.hpp index 33dcc10..6f4fbbf 100644 --- a/src/player.hpp +++ b/src/player.hpp @@ -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 diff --git a/src/spawner.cpp b/src/spawner.cpp new file mode 100644 index 0000000..4847fbd --- /dev/null +++ b/src/spawner.cpp @@ -0,0 +1,24 @@ +#include "spawner.hpp" +#include "truck.hpp" +#include +#include + +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(std::rand() % 2 != 0)}; +} diff --git a/src/spawner.hpp b/src/spawner.hpp new file mode 100644 index 0000000..c0a08f9 --- /dev/null +++ b/src/spawner.hpp @@ -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 diff --git a/src/truck.cpp b/src/truck.cpp index 7522c3d..89466eb 100644 --- a/src/truck.cpp +++ b/src/truck.cpp @@ -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 +#include #include Truck::Truck(bool left) -: ce::CollidableNode("truck", 0x1u, 0x1u) -, sprite{this->create_child("sprite", "truck")} -, shape{this->create_child("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("truck_sprite", "truck")} { + this->create_child("truck_col_shape_0", 0.6f, 0.1f)->set_transform(ce::Transform().translated({0.f, 0.5f})); + this->create_child("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(); } diff --git a/src/truck.hpp b/src/truck.hpp index 85642d1..b945128 100644 --- a/src/truck.hpp +++ b/src/truck.hpp @@ -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