summaryrefslogtreecommitdiff
path: root/platformer/src/player.h
blob: b39f4c5544b22e352092c0d99bdc22f78c184544 (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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
/*
 * ============================================================
 * 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)
 *       PLAYER_SKID,     on ground, braking after pulling the stick back at
 *                        full run; retreating_movement is on
 *       PLAYER_WALL_HANG in air, clinging to a hangable wall and sliding down
 *   } PlayerState;
 *
 *   typedef enum JumpKind {       which jump the player is in the air from
 *       JUMP_NONE,       on the ground, or airborne without jumping
 *       JUMP_NORMAL,     a normal jump
 *       JUMP_SECOND,     the higher jump from heightened_movement
 *       JUMP_RETREAT     the jump out of a skid, from retreating_movement
 *   } JumpKind;
 *
 *   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
 *                                   any landing except the second jump's (normal
 *                                   or retreat jump, or a plain fall), and for the
 *                                   whole second jump, retreat jump, wall hang and
 *                                   every wall jump (NOT a plain first jump). On the ground, A gives the second
 *                                   jump; in the air, it lets the player wall hang.
 *       float       heightened_timer;     s left before heightened_movement ends
 *                                   (not counted down during the second jump)
 *       bool        retreating_movement;  on for the whole skid (PLAYER_SKID) and
 *                                   the retreat jump out of it. While skidding, A
 *                                   gives the retreat jump. Later moves will read
 *                                   it too.
 *       JumpKind    jump_kind;      which jump the player is in the air from
 *       float       skid_time;      s since the current skid began
 *       float       skid_speed;     forward_speed when the skid began
 *       float       skid_yaw;       direction the skid slides (the old run
 *                                   direction; the player already faces the stick)
 *       float       visual_offset;  m the drawn height is off the body's feet after a
 *                                   stair step (physics steps instantly; this eases
 *                                   back to 0 at STEP_SMOOTH_RATE so it looks smooth)
 *       float       jump_buffer;    s left on the last A press: while > 0, the first
 *                                   moment a jump is possible, it happens
 *       float       wall_time;      s since the current wall hang began
 *       float       wall_jump_yaw;  where a wall jump from this hang launches: the
 *                                   approach direction mirrored off the wall
 *       bool        has_last_wall;  a wall was jumped off since the last landing
 *       Vec3        last_wall_normal; that wall's surface: which way it faces...
 *       float       last_wall_plane;  ...and where its face lies along that
 *                                   direction. That surface can't be hung on
 *                                   again until the player lands (no climbing
 *                                   one wall, even one built from several boxes)
 *   } 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);
 *   Vec3        player_visual_pos(const Player *p);
 *                   Where to DRAW the player and point the camera: body.pos,
 *                   but with stair steps smoothed out (see visual_offset).
 *                   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, the
 *              skid's retreat jump and the wall hang (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
 * and retreat jumps didn't need one: they're a JUMP with a different launch
 * (see JumpKind). The skid and wall hang did, because each follows its own
 * movement rules. */
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) */
    PLAYER_SKID,     /* on ground, braking after pulling back at full run */
    PLAYER_WALL_HANG /* in air, clinging to a hangable wall and sliding down */
} PlayerState;

/* Which jump the player is in the air from. It decides the top speed in the
 * air and what landing turns on or off. */
typedef enum JumpKind {
    JUMP_NONE,       /* on the ground, or airborne without jumping (walked off an edge) */
    JUMP_NORMAL,     /* a normal jump */
    JUMP_SECOND,     /* the higher jump from heightened_movement */
    JUMP_RETREAT     /* the jump out of a skid, from retreating_movement */
} JumpKind;

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 window, and on through second/retreat/wall jumps and hangs */
    float       heightened_timer;    /* s left before heightened_movement ends; paused during the second jump */
    bool        retreating_movement; /* skidding (or in the jump out of a skid); later moves read it too */
    JumpKind    jump_kind;           /* which jump the player is in the air from */
    float       skid_time;           /* s since the current skid began */
    float       skid_speed;          /* forward_speed when the skid began */
    float       skid_yaw;            /* direction the skid slides; facing_yaw already points at the stick */
    float       visual_offset;       /* m the drawn/followed height lags the body after stair steps; eases to 0 */
    float       jump_buffer;         /* s left on the last A press; a jump fires as soon as one is possible */
    float       wall_time;           /* s since the current wall hang began */
    float       wall_jump_yaw;       /* a wall jump's launch direction: the approach mirrored off the wall */
    bool        has_last_wall;       /* a wall was jumped off since the last landing... */
    Vec3        last_wall_normal;    /* ...facing this way... */
    float       last_wall_plane;     /* ...with its face here along that direction; no regrab until landing */
} 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);
Vec3        player_visual_pos(const Player *p);
const char *player_state_name(PlayerState s);

#endif /* PLAYER_H */