summaryrefslogtreecommitdiff
path: root/kon/konplatformer.h
blob: 50e6006715a64e91f3f5662c255832a562caffc7 (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
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
/* SPDX-License-Identifier: MIT */
/*
 * konplatformer.h
 *
 * Copyright (c) 2026 KonAki
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in all
 * copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
 * SOFTWARE.
 */

#ifndef KONPLATFORMER_H
#define KONPLATFORMER_H

#include "koncollide.h"
#include "kontile.h"

#ifdef __cplusplus
extern "C" {
#endif

/*** platformer ***/

/* a box that runs and jumps through a tilemap. the game sets moveX and jumpHeld every frame, calls kon_platformerUpdate and reads
   box, velocity and onGround back. everything is in map pixels and seconds. kon_platformerInit fills in the tuning values,
   change them afterwards as you like */
typedef struct kon_platformer {
	kon_box2_t box;
	kon_vec2_t velocity;
	int onGround;

	int moveX; /* -1 left, 0 stand, 1 right */
	int jumpHeld;

	float runSpeed;
	float groundAccel, airAccel; /* how fast velocity.x closes in on the run speed, per second */
	float gravity;
	float jumpSpeed;
	float jumpCut; /* letting go of jump early slows the rise to this speed */
	float maxFall;
	float coyoteTime; /* seconds after leaving a ledge a jump still works */
	float bufferTime; /* seconds before landing a jump press is remembered */

	/* private */
	float coyote, buffer, leftover;
	int jumpWas;
} kon_platformer_t;

void kon_platformerInit(kon_platformer_t *p, kon_box2_t box);

/* one step of dt seconds, dt should be small and steady. returns the KON_HIT_ flags of the move */
int kon_platformerStep(kon_platformer_t *p, const kon_tilemap_t *map, const uint8_t *solid, int solidCount, float dt);

/* takes the frame time and runs steps of 1/120 of a second, so the jump feels the same at any frame rate */
void kon_platformerUpdate(kon_platformer_t *p, const kon_tilemap_t *map, const uint8_t *solid, int solidCount, double dt);


#ifdef __cplusplus
}
#endif
#endif