tomo/examples/game/player.tm

29 lines
918 B
Plaintext
Raw Normal View History

2024-09-08 16:49:47 -07:00
# Defines a struct representing the player, which is controlled by WASD keys
use ./world.tm
use ./raylib.tm
2024-09-08 16:49:47 -07:00
struct Player(pos,prev_pos:Vector2):
WALK_SPEED := Num32(500.)
ACCEL := Num32(0.3)
FRICTION := Num32(0.99)
SIZE := Vector2(30, 30)
COLOR := Color(0x60, 0x60, 0xbF)
2024-09-08 16:49:47 -07:00
2024-10-27 17:49:03 -07:00
func update(p:@Player):
target_x := inline C:Num32 {
(Num32_t)((IsKeyDown(KEY_A) ? -1 : 0) + (IsKeyDown(KEY_D) ? 1 : 0))
}
target_y := inline C:Num32 {
(Num32_t)((IsKeyDown(KEY_W) ? -1 : 0) + (IsKeyDown(KEY_S) ? 1 : 0))
}
target_vel := Vector2(target_x, target_y):norm() * Player.WALK_SPEED
2024-09-08 16:49:47 -07:00
vel := (p.pos - p.prev_pos)/World.DT
2025-03-10 10:42:57 -07:00
vel *= Player.FRICTION
vel = vel:mix(target_vel, Player.ACCEL)
2024-09-08 16:49:47 -07:00
p.prev_pos, p.pos = p.pos, p.pos + World.DT*vel
2024-09-08 17:26:33 -07:00
func draw(p:Player):
2025-03-11 15:02:13 -07:00
DrawRectangleV(p.pos, Player.SIZE, Player.COLOR)