tomo/examples/game/player.tm

32 lines
866 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 libraylib.so
use <raylib.h>
use <raymath.h>
use vectors
use ./world.tm
struct Player(pos,prev_pos:Vec2):
WALK_SPEED := 500.
ACCEL := 0.3
FRICTION := 0.99
2024-09-08 17:25:06 -07:00
SIZE := Vec2(30, 30)
2024-09-08 16:49:47 -07:00
func update(p:&Player):
target_x := inline C:Num {
2024-09-08 16:49:47 -07:00
(Num_t)((IsKeyDown(KEY_A) ? -1 : 0) + (IsKeyDown(KEY_D) ? 1 : 0))
}
target_y := inline C:Num {
2024-09-08 16:49:47 -07:00
(Num_t)((IsKeyDown(KEY_W) ? -1 : 0) + (IsKeyDown(KEY_S) ? 1 : 0))
}
2024-09-08 16:49:47 -07:00
target_vel := Vec2(target_x, target_y):norm() * WALK_SPEED
vel := (p.pos - p.prev_pos)/World.DT
vel *= FRICTION
vel = vel:mix(target_vel, ACCEL)
p.prev_pos, p.pos = p.pos, p.pos + World.DT*vel
2024-09-08 17:26:33 -07:00
func draw(p:Player):
2024-09-08 17:25:06 -07:00
Color.PLAYER:draw_rectangle(p.pos, Player.SIZE)