aboutsummaryrefslogtreecommitdiff
path: root/examples/game/player.tm
blob: 5909ae63ad801207b1397f87fcf09539856206e3 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# 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
    SIZE := Vec2(30, 30)

    func update(p:&Player):
        target_x := inline C:Num {
            (Num_t)((IsKeyDown(KEY_A) ? -1 : 0) + (IsKeyDown(KEY_D) ? 1 : 0))
        }
        target_y := inline C:Num {
            (Num_t)((IsKeyDown(KEY_W) ? -1 : 0) + (IsKeyDown(KEY_S) ? 1 : 0))
        }
        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

    func draw(p:Player):
        Color.PLAYER:draw_rectangle(p.pos, Player.SIZE)