Arkadodge/pad.odin

66 lines
1.6 KiB
Odin
Raw Normal View History

2024-09-08 03:39:03 +03:00
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,
2024-09-09 23:41:16 +03:00
health: f32,
2024-09-08 03:39:03 +03:00
}
pad_update :: proc(pad: ^Pad, delta: f32) {
2024-09-09 23:41:16 +03:00
pad.health = min(pad.health + delta, 100)
2024-09-08 03:39:03 +03:00
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
}
2024-09-09 23:41:16 +03:00
}
pad_draw :: proc(pad: ^Pad) {
rl.DrawRectanglePro(rl.Rectangle{pad.position.x, pad.position.y, pad.size.x, pad.size.y}, pad.size / 2, 0, rl.RED)
hbar_x := pad.health / 100 * pad.size.x
rl.DrawRectanglePro(rl.Rectangle{pad.position.x, pad.position.y, hbar_x, pad.size.y/2}, {hbar_x / 2, pad.size.y / 4}, 0, rl.GREEN)
2024-09-08 03:39:03 +03:00
}