use sevenx_engine::{
collision::Collider,
game_object::{Direction, GameObject, ObjectType, PlayerAction},
input::InputHandler,
physics::Physics,
sprite::SpriteSheet,
world::World,
Engine, EngineConfig, GameState, KeyCode,
};
struct PhysicsDemo {
spawn_timer: f32,
}
impl GameState for PhysicsDemo {
fn new() -> Self {
PhysicsDemo { spawn_timer: 0.0 }
}
fn update(&mut self, dt: f32, input: &InputHandler, world: &mut World) {
if let Some(player) = world
.game_objects
.iter_mut()
.find(|obj| obj.object_type == ObjectType::Player && obj.active)
{
let speed = if input.is_shift_pressed() { 450.0 } else { 300.0 };
let jump_force = -500.0;
let axis_x = input.get_axis_horizontal();
if axis_x != 0.0 {
if let Some(physics) = &mut player.physics {
physics.velocity.0 = axis_x * speed;
}
player.facing_direction = if axis_x < 0.0 { Direction::Left } else { Direction::Right };
player.current_action = PlayerAction::Walk;
} else {
if let Some(physics) = &mut player.physics {
physics.velocity.0 = 0.0;
}
player.current_action = PlayerAction::Idle;
}
if input.is_key_just_pressed(KeyCode::Space) {
if let Some(physics) = &mut player.physics {
if player.transform.position.1 >= 500.0 {
physics.velocity.1 = jump_force;
println!("🦘 Pulo!");
}
}
}
if player.transform.position.1 > 500.0 {
player.transform.position.1 = 500.0;
if let Some(physics) = &mut player.physics {
physics.velocity.1 = 0.0;
}
}
}
self.spawn_timer += dt;
if self.spawn_timer > 2.0 {
self.spawn_timer = 0.0;
self.spawn_coin(world);
}
world.game_objects.retain(|obj| {
if obj.object_type == ObjectType::Coin {
obj.transform.position.1 < 700.0
} else {
true
}
});
}
fn draw(&mut self, _world: &World, _pixels: &mut [u8]) {
}
}
impl PhysicsDemo {
fn spawn_coin(&self, world: &mut World) {
use rand::Rng;
let mut rng = rand::thread_rng();
let x = rng.gen_range(50.0..750.0);
let mut coin = GameObject::new(x, -50.0, ObjectType::Coin)
.with_physics(Physics::new().with_gravity(1.0).with_drag(0.1))
.with_collider(Collider::new(32.0, 32.0).as_trigger())
.with_z_index(5);
let coin_sprite = SpriteSheet::create_placeholder();
coin.sheets.insert(PlayerAction::Idle, coin_sprite);
world.add_object(coin);
}
}
fn main() {
println!("🎮 SevenX Engine v0.2.7 - Physics Demo");
println!("🎯 Controles:");
println!(" A/D ou Setas - Mover");
println!(" Shift - Correr mais rápido");
println!(" Espaço - Pular");
println!();
let config = EngineConfig::new()
.with_title("SevenX Engine v0.2.7 - Physics Demo")
.with_size(800, 600)
.with_gravity(980.0);
Engine::with_config(config).run::<PhysicsDemo>();
}