Continued player code, need fix the weird physics.

This commit is contained in:
KikooDX 2021-03-04 16:19:15 +01:00
parent 5bf44f8ef8
commit acd1e8d961
5 changed files with 32 additions and 12 deletions

View File

@ -6,13 +6,17 @@
* included in all copies and substantial portions of the software. */
#pragma once
#include "conf.h"
#include "tiles.h"
#include "vec2.h"
#include "tiles.h"
#include "lazyint.h"
typedef struct Level{
tile_t content[LEVEL_WIDTH * LEVEL_HEIGHT];
Vec2 start_pos;
} Level;
#include "player.h"
void level_load(Level *level, Player *player, u8 id);
void level_draw(Level level);
tile_t level_get_tile_at_px(Level level, Vec2 pos);

View File

@ -8,7 +8,6 @@
#include <stdbool.h>
#include "lazyint.h"
#include "vec2.h"
#include "level.h"
#include "input.h"
#define PLAYER_WIDTH 12
@ -39,6 +38,8 @@ typedef struct Player{
u8 coyot;
} Player;
#include "level.h"
Player player_init();
void player_update(Player *player, Level *level, Input input, u8 *level_id);
void player_draw(Player player);

View File

@ -5,10 +5,12 @@
* which is MIT licensed. The MIT license requires this copyright notice to be
* included in all copies and substantial portions of the software. */
#include <gint/display.h>
#include <gint/std/string.h>
#include "lazyint.h"
#include "level.h"
#include "tiles.h"
#include "conf.h"
#include "player.h"
#include "vec2.h"
void level_draw(Level level) {
@ -32,6 +34,13 @@ void level_draw(Level level) {
}
}
void level_load(Level *level, Player *player, u8 id) {
extern Level levels[LEVEL_SIZE];
memcpy(level->content, levels[id].content, LEVEL_SIZE);
level->start_pos = levels[id].start_pos;
player->pos = levels[id].start_pos;
}
tile_t level_get_tile_at_px(Level level, Vec2 pos) {
/* Out of bounds check. */
if (pos.x < 0 || pos.y < 0)

View File

@ -7,7 +7,6 @@
#include <gint/display.h>
#include <gint/timer.h>
#include <gint/clock.h>
#include <gint/std/string.h>
#include <stdbool.h>
#include "lazyint.h"
#include "conf.h"
@ -15,13 +14,6 @@
#include "level.h"
#include "player.h"
void load_level(Level *level, Player *player, u8 id) {
extern Level levels[LEVEL_SIZE];
memcpy(level->content, levels[id].content, LEVEL_SIZE);
level->start_pos = levels[id].start_pos;
player->pos = levels[id].start_pos;
}
int callback(volatile void *arg) {
volatile bool *has_ticked = arg;
*has_ticked = true;
@ -34,7 +26,7 @@ int main(void) {
/* Initialize level. */
Level level = (Level){};
u8 level_id = 0;
load_level(&level, &player, level_id);
level_load(&level, &player, level_id);
/* Initialize input. */
Input input = (Input){};
input_init(&input);

View File

@ -16,7 +16,7 @@
Player player_init() {
return (Player){
.pos = (Vec2){},
.pos = (Vec2){0, 0},
.spd_x = 0.0,
.spd_y = 0.0,
.facing = 1,
@ -113,6 +113,20 @@ void player_update(Player *player, Level *level, Input input, u8 *level_id) {
if (on_ground)
player->stun = false;
}
/* Return to last checkpoint if out of bounds. */
if ((player->pos.x <= -PLAYER_WIDTH) ||
(player->pos.y <= -PLAYER_HEIGHT) ||
(player->pos.x >= LEVEL_WIDTH * TILE_SIZE) ||
(player->pos.y >= LEVEL_HEIGHT * TILE_SIZE))
{
player_init(player);
player->pos = level->start_pos;
}
/* Exit, victory! */
if (player->keys_left == 0 && player_collide(*level, player->pos, EXIT_TILE, 0)) {
*level_id += 1;
level_load(level, player, *level_id);
}
}
void player_draw(Player player) {