Color fix, level and player logic progress.

This commit is contained in:
KikooDX 2021-03-02 15:09:35 +01:00
parent b35cd6e9aa
commit 70bd94b856
6 changed files with 66 additions and 9 deletions

View File

@ -16,3 +16,4 @@ typedef struct Level{
} Level;
void level_draw(Level level);
tile_t level_get_tile_at_px(Level level, Vec2 pos);

View File

@ -6,7 +6,9 @@
* included in all copies and substantial portions of the software. */
#pragma once
#include <stdint.h>
#include <stdbool.h>
#include "vec2.h"
#include "level.h"
typedef struct Player{
Vec2 pos;
@ -20,3 +22,7 @@ typedef struct Player{
} Player;
Player player_init();
void player_update(Player *player, Level *level, uint8_t *level_id);
void player_draw(Player player);
/* Helper functions. */
int8_t sign(vec2_int_t value);

View File

@ -8,6 +8,7 @@
#include <stdint.h>
#include "level.h"
#include "tiles.h"
#include "vec2.h"
void level_draw(Level level) {
/* Pixel position (where we draw). */
@ -30,3 +31,23 @@ void level_draw(Level level) {
}
}
tile_t level_get_tile_at_px(Level level, Vec2 pos) {
/* Out of bounds check. */
if (pos.x < 0 || pos.y < 0)
return OUT_OF_BOUNDS;
/* Convert to unsigned integers. */
uint8_t x = pos.x / TILE_SIZE;
uint8_t y = pos.y / TILE_SIZE;
/* Out of bounds check. */
if (pos.x >= LEVEL_WIDTH || pos.y >= LEVEL_HEIGHT)
return OUT_OF_BOUNDS;
/* If everything is OK, get the tile and return it. */
return level.content[x + y * LEVEL_WIDTH];
}
void level_set_tile_at_px(Level *level, Vec2 pos, tile_t tile) {
/* Convert to unsigned integers. */
uint8_t x = pos.x / TILE_SIZE;
uint8_t y = pos.y / TILE_SIZE;
level->content[x + y * LEVEL_WIDTH] = tile;
}

View File

@ -53,12 +53,12 @@ int main(void) {
has_ticked = false;
/* Update. */
input_update(&input);
/* player_update(&player, &level, &level_id); */
player_update(&player, &level, &level_id);
}
/* Draw. */
dclear(C_BLACK);
/* player_draw(player); */
level_draw(level);
player_draw(player);
dupdate();
}

View File

@ -5,9 +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 <stdint.h>
#include <stdbool.h>
#include "player.h"
#include "conf.h"
#include "vec2.h"
#include "level.h"
Player player_init() {
return (Player){
@ -21,3 +24,29 @@ Player player_init() {
.coyot = 0,
};
}
void player_update(Player *player, Level *level, uint8_t *level_id) {
}
void player_draw(Player player) {
/* Draw colored rectangle depending on player state. */
int color = 0;
if (player.stun)
color = C_RGB(230/4, 41/4, 55/4); /* Red. */
else
color = C_RGB(0/4, 121/4, 241/4); /* Blue. */
const vec2_int_t x = player.pos.x + DRAW_OFFSET_X;
const vec2_int_t y = player.pos.y + DRAW_OFFSET_Y;
drect(x, y, x + PLAYER_WIDTH - 1, y + PLAYER_HEIGHT - 1, color);
}
/* Helper functions */
int8_t sign(vec2_int_t value) {
if (value > 0)
return 1;
else if (value < 0)
return -1;
else
return 0;
}

View File

@ -10,13 +10,13 @@
int tile_color(tile_t tile) {
int color = 0;
switch (tile) {
case AIR_TILE: color = C_RGB(0, 0, 0); break;
case SOLID_TILE: color = C_RGB(10, 210, 180); break;
case PAIN_TILE: color = C_RGB(210, 10, 180); break;
case SPAWN_TILE: color = C_RGB(20, 220, 20); break;
case EXIT_TILE: color = C_RGB(250, 220, 10); break;
case KEY_TILE: color = C_RGB(210, 210, 210); break;
case SEMI_SOLID: color = C_RGB(5, 105, 90); break;
case AIR_TILE: color = C_RGB(0/4, 0/4, 0/4); break;
case SOLID_TILE: color = C_RGB(10/4, 210/4, 180/4); break;
case PAIN_TILE: color = C_RGB(210/4, 10/4, 180/4); break;
case SPAWN_TILE: color = C_RGB(20/4, 220/4, 20/4); break;
case EXIT_TILE: color = C_RGB(250/4, 220/4, 10/4); break;
case KEY_TILE: color = C_RGB(210/4, 210/4, 210/4); break;
case SEMI_SOLID: color = C_RGB(5/4, 105/4, 90/4); break;
}
return color;
}