diff options
| author | Bruce Hill <bruce@bruce-hill.com> | 2024-09-08 19:49:47 -0400 |
|---|---|---|
| committer | Bruce Hill <bruce@bruce-hill.com> | 2024-09-08 19:49:47 -0400 |
| commit | a7ae25ec086117e133b2dfbbc1c5025aa2f29964 (patch) | |
| tree | 45bcab3728e0998205e280a0dd84a44bcb2925be /examples/game/player.tm | |
| parent | f86cc6549ff6075c3963fce819391d8d8d6960dc (diff) | |
Add example game using raylib
Diffstat (limited to 'examples/game/player.tm')
| -rw-r--r-- | examples/game/player.tm | 31 |
1 files changed, 31 insertions, 0 deletions
diff --git a/examples/game/player.tm b/examples/game/player.tm new file mode 100644 index 00000000..5aff9ea5 --- /dev/null +++ b/examples/game/player.tm @@ -0,0 +1,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(50, 50) + + func update(p:&Player): + target_x := inline C ( + (Num_t)((IsKeyDown(KEY_A) ? -1 : 0) + (IsKeyDown(KEY_D) ? 1 : 0)) + ) : Num + target_y := inline C ( + (Num_t)((IsKeyDown(KEY_W) ? -1 : 0) + (IsKeyDown(KEY_S) ? 1 : 0)) + ) : Num + 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.RED:draw_rectangle(p.pos, Player.SIZE) |
