package main import rl "vendor:raylib" import "core:math" PAD_MAX_SPEED : f32 = 600 PAD_ACCELERATION : f32 = 2000 PAD_ACCELERATION_REV : f32 = 2900 PAD_DECCELERATION : f32 = 2000 Pad :: struct { position: Vec2, velocity: Vec2, size: Vec2, } pad_update :: proc(pad: ^Pad, delta: f32) { target : f32 = 0 if rl.IsKeyDown(rl.KeyboardKey.LEFT) { target = -PAD_MAX_SPEED } if rl.IsKeyDown(rl.KeyboardKey.RIGHT) { target = PAD_MAX_SPEED } target_sign := math.sign(target) if target != 0 { accel : f32 = PAD_ACCELERATION if pad.velocity.x * target < 0 { accel = PAD_ACCELERATION_REV } pad.velocity.x = clamp(pad.velocity.x + accel * target_sign * delta, -PAD_MAX_SPEED, PAD_MAX_SPEED) } else { vel_sign := math.sign(pad.velocity.x) deccel := PAD_DECCELERATION * vel_sign * delta if math.abs(pad.velocity.x) > math.abs(deccel) { pad.velocity.x -= deccel } else { pad.velocity.x = 0 } } pad.position += pad.velocity * delta pad_half_width := pad.size.x / 2 if pad.position.x < pad_half_width { pad.position.x = pad_half_width pad.velocity.x = 0 } if pad.position.x > GameField.x - pad_half_width { pad.position.x = GameField.x - pad_half_width pad.velocity.x = 0 } }