Added a room matrix that deals with movement and collisions.

This commit is contained in:
Linus Probert 2017-12-05 15:03:20 +01:00
parent 2a88233e82
commit bdc5b6d629
10 changed files with 171 additions and 31 deletions

View File

@ -26,6 +26,8 @@ add_executable(breakhack
src/map_lua
src/camera
src/timer
src/roommatrix
src/position
)
target_link_libraries(breakhack

13
src/defines.h Normal file
View File

@ -0,0 +1,13 @@
#ifndef DEFINES_H_
#define DEFINES_H_
/* Room/Map dimensions */
#define MAP_ROOM_HEIGHT 12
#define MAP_ROOM_WIDTH 16
#define MAP_V_ROOM_COUNT 10
#define MAP_H_ROOM_COUNT 10
#define TILE_DIMENSION 64
#endif // DEFINES_H_

View File

@ -11,6 +11,7 @@
#include "map.h"
#include "map_lua.h"
#include "timer.h"
#include "roommatrix.h"
#define SCREEN_WIDTH 1024
#define SCREEN_HEIGHT 768
@ -20,6 +21,7 @@ static SDL_Renderer *gRenderer = NULL;
static Sprite *gPlayer = NULL;
static LinkedList *gSpriteList = NULL;
static Map *gMap = NULL;
static RoomMatrix *gRoomMatrix = NULL;
static Camera gCamera;
@ -92,6 +94,7 @@ bool init()
if (result) {
gCamera.pos = (Position) { 0, 0 };
gCamera.renderer = gRenderer;
gRoomMatrix = roommatrix_create();
}
return result;
}
@ -112,7 +115,7 @@ bool handle_events()
if (event.type == SDL_QUIT) {
quit = true;
} else {
gPlayer->handle_event(gPlayer, &event);
gPlayer->handle_event(gPlayer, gRoomMatrix, &event);
camera_follow_position(&gCamera, &gPlayer->pos);
map_set_current_room(gMap, &gPlayer->pos);
}
@ -131,6 +134,7 @@ void run()
timer_start(fpsTimer);
quit = handle_events();
roommatrix_populate_from_map(gRoomMatrix, gMap);
SDL_RenderClear(gRenderer);

View File

@ -8,24 +8,19 @@
#include "sprite.h"
#include "camera.h"
#include "position.h"
#include "defines.h"
#define MAP_ROOM_HEIGHT 12
#define MAP_ROOM_WIDTH 16
#define MAP_V_ROOM_COUNT 10
#define MAP_H_ROOM_COUNT 10
#define TILE_DIMENSION 64
typedef struct {
typedef struct MapTile_t {
unsigned int textureIndex;
SDL_Rect clip;
bool collider;
} MapTile;
typedef struct {
typedef struct Room_t {
MapTile* tiles[MAP_ROOM_WIDTH][MAP_ROOM_HEIGHT];
} Room;
typedef struct {
typedef struct Map_t {
Room* rooms[MAP_H_ROOM_COUNT][MAP_V_ROOM_COUNT];
LinkedList *textures;
Position currentRoom;

View File

@ -2,30 +2,67 @@
#include "player.h"
static
void handle_player_input(Sprite *sprite, SDL_Event *event)
bool has_collided(Sprite *sprite, RoomMatrix *matrix)
{
Position pos = position_to_matrix_coords(&sprite->pos);
return matrix->spaces[pos.x][pos.y].occupied;
}
static
void move_left(Sprite *sprite, RoomMatrix *matrix)
{
sprite->texture->clip.y = 16;
sprite->pos.x -= TILE_DIMENSION;
if (has_collided(sprite, matrix))
sprite->pos.x += TILE_DIMENSION;
}
static
void move_right(Sprite *sprite, RoomMatrix *matrix)
{
sprite->texture->clip.y = 32;
sprite->pos.x += TILE_DIMENSION;
if (has_collided(sprite, matrix))
sprite->pos.x -= TILE_DIMENSION;
}
static
void move_up(Sprite *sprite, RoomMatrix *matrix)
{
sprite->texture->clip.y = 48;
sprite->pos.y -= TILE_DIMENSION;
if (has_collided(sprite, matrix))
sprite->pos.y += TILE_DIMENSION;
}
static
void move_down(Sprite *sprite, RoomMatrix *matrix)
{
sprite->texture->clip.y = 0;
sprite->pos.y += TILE_DIMENSION;
if (has_collided(sprite, matrix))
sprite->pos.y -= TILE_DIMENSION;
}
static
void handle_player_input(Sprite *sprite, RoomMatrix *matrix, SDL_Event *event)
{
unsigned int width = sprite->texture->dim.width;
unsigned int height = sprite->texture->dim.height;
static unsigned int step = 1;
if (event->type == SDL_KEYDOWN) {
switch (event->key.keysym.sym) {
case SDLK_LEFT:
sprite->texture->clip.y = 16;
sprite->pos.x -= width;
break;
case SDLK_RIGHT:
sprite->texture->clip.y = 32;
sprite->pos.x += width;
break;
case SDLK_UP:
sprite->texture->clip.y = 48;
sprite->pos.y -= height;
break;
case SDLK_DOWN:
sprite->texture->clip.y = 0;
sprite->pos.y += height;
break;
case SDLK_LEFT:
move_left(sprite, matrix);
break;
case SDLK_RIGHT:
move_right(sprite, matrix);
break;
case SDLK_UP:
move_up(sprite, matrix);
break;
case SDLK_DOWN:
move_down(sprite, matrix);
break;
}
sprite->texture->clip.x = 16*step;
if (step == 3)

16
src/position.c Normal file
View File

@ -0,0 +1,16 @@
#include "position.h"
#include "defines.h"
Position position_to_matrix_coords(Position *src)
{
unsigned int room_px_width, room_px_height;
Position pos;
room_px_width = TILE_DIMENSION * MAP_ROOM_WIDTH;
room_px_height = TILE_DIMENSION * MAP_ROOM_HEIGHT;
pos.x = (src->x % room_px_width) / TILE_DIMENSION;
pos.y = (src->y % room_px_height) / TILE_DIMENSION;
return pos;
}

View File

@ -6,4 +6,6 @@ typedef struct {
int y;
} Position;
Position position_to_matrix_coords(Position*);
#endif // POSITION_H_

42
src/roommatrix.c Normal file
View File

@ -0,0 +1,42 @@
#include "roommatrix.h"
#include "util.h"
#include "map.h"
RoomMatrix* roommatrix_create()
{
RoomMatrix *m = ec_malloc(sizeof(RoomMatrix));
roommatrix_reset(m);
return m;
}
void roommatrix_populate_from_map(RoomMatrix *rm, Map *m)
{
int i, j;
Room *r;
r = m->rooms[m->currentRoom.x][m->currentRoom.y];
for (i = 0; i < MAP_ROOM_WIDTH; ++i) {
for (j = 0; j < MAP_ROOM_HEIGHT; ++j) {
rm->spaces[i][j].occupied = r->tiles[i][j]->collider;
}
}
}
void roommatrix_reset(RoomMatrix *m)
{
int i, j;
for (i = 0; i < MAP_ROOM_WIDTH; ++i) {
for (j = 0; j < MAP_ROOM_HEIGHT; ++j) {
m->spaces[i][j].occupied = false;
m->spaces[i][j].character = NULL;
m->spaces[i][j].player = NULL;
}
}
}
void roommatrix_destroy(RoomMatrix *m)
{
free(m);
}

28
src/roommatrix.h Normal file
View File

@ -0,0 +1,28 @@
#ifndef ROOMMATRIX_H_
#define ROOMMATRIX_H_
#include <stdbool.h>
#include "defines.h"
typedef struct Sprite_t Sprite;
typedef struct Map_t Map;
typedef struct {
bool occupied;
Sprite* character;
Sprite* player;
} RoomSpace;
typedef struct {
RoomSpace spaces[MAP_ROOM_WIDTH][MAP_ROOM_HEIGHT];
} RoomMatrix;
RoomMatrix* roommatrix_create();
void roommatrix_populate_from_map(RoomMatrix*, Map*);
void roommatrix_reset(RoomMatrix*);
void roommatrix_destroy(RoomMatrix*);
#endif // ROOMMATRIX_H_

View File

@ -6,11 +6,12 @@
#include "texture.h"
#include "position.h"
#include "camera.h"
#include "roommatrix.h"
typedef struct Sprite_t{
typedef struct Sprite_t {
Texture *texture;
Position pos;
void (*handle_event)(struct Sprite_t *sprite, SDL_Event*);
void (*handle_event)(struct Sprite_t*, RoomMatrix*, SDL_Event*);
} Sprite;
Sprite* sprite_create();