From 00a2b54759a30f77c6200e767de683e67cc4c174 Mon Sep 17 00:00:00 2001 From: Linus Probert Date: Wed, 13 Dec 2017 23:20:54 +0100 Subject: [PATCH] Lua bindings for monsters. Monsters in matrix. So far I just add one black ant. More to come. --- TODO.txt | 3 ++- data/mapgen.lua | 3 +++ data/monstergen.lua | 15 +++++++++++++ hashtable/hashtable.c | 9 ++++---- hashtable/hashtable.h | 4 ++-- src/map.c | 4 ++-- src/map.h | 2 +- src/map_lua.c | 51 +++++++++++++++++++++++++++++++++++++++++++ src/monster.c | 3 +++ src/monster.h | 1 + src/player.c | 4 ++++ src/position.c | 22 +++++++++++++++++++ src/position.h | 4 ++++ src/roommatrix.c | 13 ++++++++++- src/sprite.c | 21 +++++++++--------- src/sprite.h | 1 + 16 files changed, 138 insertions(+), 22 deletions(-) create mode 100644 data/monstergen.lua diff --git a/TODO.txt b/TODO.txt index c8f3fff..0016643 100644 --- a/TODO.txt +++ b/TODO.txt @@ -2,7 +2,8 @@ x Add walls and stuff to maps x Implement simple box collisions o Add enemies (generated through lua) x Monster object created - - Lua bindings for creation + x Lua bindings for creation + o Making some better generation and randomeness - Hitting enemies - Moving enemies - Lower levels diff --git a/data/mapgen.lua b/data/mapgen.lua index 3b931dc..dd105fe 100644 --- a/data/mapgen.lua +++ b/data/mapgen.lua @@ -1,4 +1,5 @@ local room_builder = require "data/maproombuilder" +local monster_gen = require "data/monstergen" -- Setting up some functions local time = os.time @@ -122,6 +123,8 @@ local map_matrix = generate_path() -- Print path [Debug] -- print_matrix(map_matrix) +monster_gen.add_monster_to_room(map); + for i=1,10 do for j=1,10 do local room = map_matrix[i][j] diff --git a/data/monstergen.lua b/data/monstergen.lua new file mode 100644 index 0000000..0e5292a --- /dev/null +++ b/data/monstergen.lua @@ -0,0 +1,15 @@ +local module = {} + +local black_ant = { + texturePath1 = "assets/Characters/Pest0.png", + texturePath2 = "assets/Characters/Pest1.png", + clipX = 16, + clipY = 64, +} + + +function module.add_monster_to_room(map) + add_monster(map, 128, 128, black_ant); +end + +return module diff --git a/hashtable/hashtable.c b/hashtable/hashtable.c index 5188ae3..b8f49b8 100644 --- a/hashtable/hashtable.c +++ b/hashtable/hashtable.c @@ -38,7 +38,7 @@ ht_create(unsigned int size) } static unsigned int -hash(Hashtable *table, char *key) +hash(Hashtable *table, const char *key) { unsigned long int hashval = 0; int i = 0; @@ -52,7 +52,7 @@ hash(Hashtable *table, char *key) } static Entry* -entry_create(char *key, void *value) +entry_create(const char *key, void *value) { Entry *entry; @@ -65,7 +65,7 @@ entry_create(char *key, void *value) } void -ht_set(Hashtable *table, char *key, void *val) +ht_set(Hashtable *table, const char *key, void *val) { int hashkey = 0; Entry *newEntry; @@ -106,7 +106,7 @@ ht_set(Hashtable *table, char *key, void *val) } void* -ht_get(Hashtable *table, char *key) +ht_get(Hashtable *table, const char *key) { int hashkey = 0; Entry *entry; @@ -149,6 +149,7 @@ ht_destroy_custom(Hashtable *table, void (*destroy_value)(void *value)) next = entry->next; destroy_value(entry->value); entry->value = NULL; + free(entry->key); free(entry); entry = next; } diff --git a/hashtable/hashtable.h b/hashtable/hashtable.h index bc4090e..94e2205 100644 --- a/hashtable/hashtable.h +++ b/hashtable/hashtable.h @@ -14,9 +14,9 @@ typedef struct table_t { Hashtable* ht_create(unsigned int size); -void ht_set(Hashtable*, char *key, void *val); +void ht_set(Hashtable*, const char *key, void *val); -void* ht_get(Hashtable*, char *key); +void* ht_get(Hashtable*, const char *key); void ht_destroy(Hashtable*); diff --git a/src/map.c b/src/map.c index 5835f5e..91dc4d0 100644 --- a/src/map.c +++ b/src/map.c @@ -62,7 +62,7 @@ void map_add_decoration(Map *map, Position *tile_pos, MapTile *tile) } Texture* -map_add_monster_texture(Map *map, char *path, SDL_Renderer *renderer) +map_add_monster_texture(Map *map, const char *path, SDL_Renderer *renderer) { Texture *t; @@ -218,7 +218,7 @@ void map_destroy(Map *map) texture_destroy(linkedlist_pop(&map->textures)); } while (map->monsters != NULL) { - monster_destroy(linkedlist_pop(&map->textures)); + monster_destroy(linkedlist_pop(&map->monsters)); } ht_destroy_custom(map->monsterTextures, (void (*)(void*)) texture_destroy); timer_destroy(map->renderTimer); diff --git a/src/map.h b/src/map.h index b855fd2..4f70743 100644 --- a/src/map.h +++ b/src/map.h @@ -46,7 +46,7 @@ void map_add_tile(Map *map, Position *tile_pos, MapTile*); void map_add_decoration(Map *map, Position *tile_pos, MapTile*); -Texture* map_add_monster_texture(Map*, char *path, SDL_Renderer*); +Texture* map_add_monster_texture(Map*, const char *path, SDL_Renderer*); void map_add_monster(Map*, Monster*); diff --git a/src/map_lua.c b/src/map_lua.c index 3f2d0a7..7958ea5 100644 --- a/src/map_lua.c +++ b/src/map_lua.c @@ -144,6 +144,54 @@ int l_add_decoration(lua_State *L) return 0; } +static int +l_add_monster(lua_State *L) +{ + Monster *monster; + Map *map; + int x, y, clip_x, clip_y; + const char *texture_path_1, *texture_path_2; + Texture *texture1, *texture2; + SDL_Renderer *renderer; + + renderer = luaL_checksdlrenderer(L); + map = luaL_checkmap(L, 1); + x = luaL_checkinteger(L, 2); + y = luaL_checkinteger(L, 3); + + // Read the table + lua_settop(L, 4); + luaL_checktype(L, 4, LUA_TTABLE); + + lua_getfield(L, 4, "texturePath1"); + lua_getfield(L, 4, "texturePath2"); + lua_getfield(L, 4, "clipX"); + lua_getfield(L, 4, "clipY"); + + texture_path_1 = luaL_checkstring(L, -4); + texture_path_2 = luaL_checkstring(L, -3); + clip_x = luaL_checkinteger(L, -2); + clip_y = luaL_checkinteger(L, -1); + + texture1 = map_add_monster_texture(map, texture_path_1, renderer); + texture2 = map_add_monster_texture(map, texture_path_2, renderer); + + texture1->dim = (Dimension) { TILE_DIMENSION, TILE_DIMENSION }; + texture2->dim = (Dimension) { TILE_DIMENSION, TILE_DIMENSION }; + + lua_pop(L, 4); + + monster = monster_create(); + monster->clip = (SDL_Rect) { clip_x, clip_y, 16, 16 }; + monster->sprite->pos = (Position) { x, y }; + sprite_set_texture(monster->sprite, texture1, 0); + sprite_set_texture(monster->sprite, texture2, 1); + + map_add_monster(map, monster); + + return 0; +} + Map* map_lua_generator_run(SDL_Renderer *renderer) { int status, result; @@ -177,6 +225,9 @@ Map* map_lua_generator_run(SDL_Renderer *renderer) lua_pushcfunction(L, l_map_set_current_room); lua_setglobal(L, "set_current_room"); + lua_pushcfunction(L, l_add_monster); + lua_setglobal(L, "add_monster"); + result = lua_pcall(L, 0, LUA_MULTRET, 0); if (result) { fprintf(stderr, "[!!] Failed to run script: %s\n", diff --git a/src/monster.c b/src/monster.c index fe354b3..91095b7 100644 --- a/src/monster.c +++ b/src/monster.c @@ -6,12 +6,15 @@ monster_create() { Monster *m = ec_malloc(sizeof(Monster)); m->sprite = sprite_create(); + m->clip = (SDL_Rect) { 0, 0, 16, 16 }; return m; } void monster_render(Monster *m, Camera *cam) { + m->sprite->textures[0]->clip = m->clip; + m->sprite->textures[1]->clip = m->clip; sprite_render(m->sprite, cam); } diff --git a/src/monster.h b/src/monster.h index 6cbd3ae..0ef9e6d 100644 --- a/src/monster.h +++ b/src/monster.h @@ -8,6 +8,7 @@ typedef struct { Sprite *sprite; Stats stats; + SDL_Rect clip; } Monster; Monster* monster_create(); diff --git a/src/player.c b/src/player.c index 7c9a753..8e95471 100644 --- a/src/player.c +++ b/src/player.c @@ -65,15 +65,19 @@ void handle_player_input(Sprite *sprite, RoomMatrix *matrix, SDL_Event *event) if (event->type == SDL_KEYDOWN) { switch (event->key.keysym.sym) { case SDLK_LEFT: + case SDLK_h: move_left(sprite, matrix); break; case SDLK_RIGHT: + case SDLK_l: move_right(sprite, matrix); break; case SDLK_UP: + case SDLK_k: move_up(sprite, matrix); break; case SDLK_DOWN: + case SDLK_j: move_down(sprite, matrix); break; } diff --git a/src/position.c b/src/position.c index 37b054d..d5ee729 100644 --- a/src/position.c +++ b/src/position.c @@ -28,3 +28,25 @@ Position position_to_room_coords(Position *src) return pos; } + +bool position_in_room(Position *pos, Position *roomPos) +{ + unsigned int room_px_width, room_px_height, room_x_px, room_y_px; + + room_px_width = TILE_DIMENSION * MAP_ROOM_WIDTH; + room_px_height = TILE_DIMENSION * MAP_ROOM_HEIGHT; + + room_x_px = roomPos->x * room_px_width; + room_y_px = roomPos->y * room_px_height; + + if (pos->x < room_x_px) + return false; + else if (pos->x > room_x_px + room_px_width) + return false; + else if (pos->y < room_y_px) + return false; + else if (pos->y > room_y_px + room_px_height) + return false; + + return true; +} diff --git a/src/position.h b/src/position.h index 5440cd9..2aa1468 100644 --- a/src/position.h +++ b/src/position.h @@ -1,6 +1,8 @@ #ifndef POSITION_H_ #define POSITION_H_ +#include + typedef struct { int x; int y; @@ -10,4 +12,6 @@ Position position_to_matrix_coords(Position*); Position position_to_room_coords(Position*); +bool position_in_room(Position *pos, Position *roomPos); + #endif // POSITION_H_ diff --git a/src/roommatrix.c b/src/roommatrix.c index 9d8d70a..301c593 100644 --- a/src/roommatrix.c +++ b/src/roommatrix.c @@ -12,8 +12,10 @@ RoomMatrix* roommatrix_create() void roommatrix_populate_from_map(RoomMatrix *rm, Map *m) { - int i, j; + int i, j, monster_count; + Position monster_matrix_pos; Room *r; + Monster *monster; roommatrix_reset(rm); @@ -32,6 +34,15 @@ void roommatrix_populate_from_map(RoomMatrix *rm, Map *m) } } } + + monster_count = linkedlist_size(m->monsters); + for (i = 0; i < monster_count; ++i) { + monster = linkedlist_get(&m->monsters, i); + if (!position_in_room(&monster->sprite->pos, &m->currentRoom)) + continue; + monster_matrix_pos = position_to_matrix_coords(&monster->sprite->pos); + rm->spaces[monster_matrix_pos.x][monster_matrix_pos.y].occupied = true; + } } static int diff --git a/src/sprite.c b/src/sprite.c index 321e372..deff83a 100644 --- a/src/sprite.c +++ b/src/sprite.c @@ -7,7 +7,7 @@ Sprite* sprite_create_default() Position pos = { 0, 0 }; Sprite *s = ec_malloc(sizeof(Sprite)); - *s = (Sprite) { { NULL, NULL }, false, pos, NULL, NULL }; + *s = (Sprite) { { NULL, NULL }, false, pos, NULL, 0, NULL }; s->renderTimer = timer_create(); @@ -50,20 +50,19 @@ sprite_set_texture(Sprite *s, Texture *t, int index) void sprite_render(Sprite *s, Camera *cam) { - static int t_index = 0; + if (s->textures[1]) { + if (!timer_started(s->renderTimer)) + timer_start(s->renderTimer); - if (!timer_started(s->renderTimer)) - timer_start(s->renderTimer); - - if (timer_get_ticks(s->renderTimer) > 300) { - timer_stop(s->renderTimer); - timer_start(s->renderTimer); - if (s->textures[1]) - t_index = t_index == 0 ? 1 : 0; + if (timer_get_ticks(s->renderTimer) > 300) { + timer_stop(s->renderTimer); + timer_start(s->renderTimer); + s->texture_index = s->texture_index == 0 ? 1 : 0; + } } Position cameraPos = camera_to_camera_position(cam, &s->pos); - texture_render(s->textures[t_index], &cameraPos, cam); + texture_render(s->textures[s->texture_index], &cameraPos, cam); } void sprite_destroy(Sprite *sprite) diff --git a/src/sprite.h b/src/sprite.h index 071ba1d..89bdd03 100644 --- a/src/sprite.h +++ b/src/sprite.h @@ -14,6 +14,7 @@ typedef struct Sprite_t { bool destroyTextures; Position pos; Timer *renderTimer; + unsigned int texture_index; void (*handle_event)(struct Sprite_t*, RoomMatrix*, SDL_Event*); } Sprite;