summaryrefslogtreecommitdiff
path: root/platformer/src/player.h
blob: 5f8d96397f43bc847925b59dc25b0f1e19f898e4 (plain) (blame)
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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
/*
 * ============================================================
 * FILE:        player.h
 *
 * PURPOSE:     Public interface for the player character: its state and the
 *              one function that advances it each tick.
 *
 * FITS IN:     main.c owns one Player, calls player_update() each tick, and
 *              calls player_reset() on Input.reset_pressed. render.c reads it
 *              to draw it.
 *
 * INTERFACE:   Use these exact names:
 *
 *   typedef enum PlayerState {
 *       PLAYER_IDLE,     on ground, not moving
 *       PLAYER_RUN,      on ground, moving (walk vs run is just speed)
 *       PLAYER_JUMP,     in air, going up after a jump
 *       PLAYER_FALL      in air, going down (or walked off something)
 *   } PlayerState;
 *
 *   typedef struct Player {
 *       Body        body;           position / velocity / on_ground (physics.h)
 *       float       facing_yaw;     direction the player faces, radians.
 *                                   Facing vector = (sin(yaw), 0, cos(yaw)),
 *                                   which matches mat4_rotate_y in math3d.h.
 *       float       forward_speed;  ground speed along facing_yaw, m/s (>= 0)
 *       PlayerState state;
 *       bool        heightened_movement;  on for PLAYER_SECOND_JUMP_WINDOW s after
 *                                   landing a normal jump, and for the whole
 *                                   second jump. While on, A gives the second
 *                                   jump. Later moves will read it too.
 *       float       heightened_timer;     s left before heightened_movement ends
 *                                   (not counted down during the second jump)
 *       int         jump_number;    which jump the player is in the air from:
 *                                   0 = none (walked off an edge, or grounded),
 *                                   1 = a normal jump, 2 = the second jump
 *   } Player;
 *
 *   void        player_init(Player *p, Vec3 spawn);
 *   void        player_reset(Player *p, Vec3 spawn);
 *   void        player_update(Player *p, const Input *in, float camera_yaw, float dt);
 *                   camera_yaw is a plain float so player.c doesn't depend on camera.h.
 *   const char *player_state_name(PlayerState s);   "IDLE", "RUN", ... for debug
 *
 *   Include "math3d.h", "physics.h", "controls.h".
 *
 * CONVENTIONS: Include guard: PLAYER_H.
 *
 * NOT IN SCOPE: Health, animation, extra moves beyond the second jump (see the
 *              README roadmap).
 *
 * DONE WHEN:   main.c and render.c compile against this header alone.
 * ============================================================
 */
#ifndef PLAYER_H
#define PLAYER_H

#include "controls.h"
#include "math3d.h"
#include "physics.h"

/* SM64-style "actions". Add states here only when a new move needs one;
 * see the README roadmap (triple jump, long jump, wall kick, ...). The second
 * jump didn't need one: it's a JUMP with a higher launch (see jump_number). */
typedef enum PlayerState {
    PLAYER_IDLE,     /* on ground, not moving */
    PLAYER_RUN,      /* on ground, moving (walk vs run is just speed) */
    PLAYER_JUMP,     /* in air, going up after a jump */
    PLAYER_FALL      /* in air, going down (or walked off something) */
} PlayerState;

typedef struct Player {
    Body        body;           /* position / velocity / on_ground */
    float       facing_yaw;     /* radians; facing vector = (sin(yaw), 0, cos(yaw)) */
    float       forward_speed;  /* ground speed along facing_yaw, m/s (>= 0) */
    PlayerState state;
    bool        heightened_movement; /* second jump available (or in progress); later moves read it too */
    float       heightened_timer;    /* s left before heightened_movement ends; paused during the second jump */
    int         jump_number;         /* airborne from: 0 = no jump (walked off an edge), 1 = normal, 2 = second */
} Player;

void        player_init(Player *p, Vec3 spawn);
void        player_reset(Player *p, Vec3 spawn);
void        player_update(Player *p, const Input *in, float camera_yaw, float dt);
const char *player_state_name(PlayerState s);

#endif /* PLAYER_H */