Lua bindings for monsters. Monsters in matrix.

So far I just add one black ant. More to come.
This commit is contained in:
Linus Probert 2017-12-13 23:20:54 +01:00
parent 95c9ed0a67
commit 00a2b54759
16 changed files with 138 additions and 22 deletions

View File

@ -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

View File

@ -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]

15
data/monstergen.lua Normal file
View File

@ -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

View File

@ -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;
}

View File

@ -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*);

View File

@ -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);

View File

@ -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*);

View File

@ -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",

View File

@ -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);
}

View File

@ -8,6 +8,7 @@
typedef struct {
Sprite *sprite;
Stats stats;
SDL_Rect clip;
} Monster;
Monster* monster_create();

View File

@ -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;
}

View File

@ -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;
}

View File

@ -1,6 +1,8 @@
#ifndef POSITION_H_
#define POSITION_H_
#include <stdbool.h>
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_

View File

@ -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

View File

@ -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)

View File

@ -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;