Lua bindings for monsters. Monsters in matrix.
So far I just add one black ant. More to come.
This commit is contained in:
parent
95c9ed0a67
commit
00a2b54759
3
TODO.txt
3
TODO.txt
|
@ -2,7 +2,8 @@ x Add walls and stuff to maps
|
||||||
x Implement simple box collisions
|
x Implement simple box collisions
|
||||||
o Add enemies (generated through lua)
|
o Add enemies (generated through lua)
|
||||||
x Monster object created
|
x Monster object created
|
||||||
- Lua bindings for creation
|
x Lua bindings for creation
|
||||||
|
o Making some better generation and randomeness
|
||||||
- Hitting enemies
|
- Hitting enemies
|
||||||
- Moving enemies
|
- Moving enemies
|
||||||
- Lower levels
|
- Lower levels
|
||||||
|
|
|
@ -1,4 +1,5 @@
|
||||||
local room_builder = require "data/maproombuilder"
|
local room_builder = require "data/maproombuilder"
|
||||||
|
local monster_gen = require "data/monstergen"
|
||||||
|
|
||||||
-- Setting up some functions
|
-- Setting up some functions
|
||||||
local time = os.time
|
local time = os.time
|
||||||
|
@ -122,6 +123,8 @@ local map_matrix = generate_path()
|
||||||
-- Print path [Debug]
|
-- Print path [Debug]
|
||||||
-- print_matrix(map_matrix)
|
-- print_matrix(map_matrix)
|
||||||
|
|
||||||
|
monster_gen.add_monster_to_room(map);
|
||||||
|
|
||||||
for i=1,10 do
|
for i=1,10 do
|
||||||
for j=1,10 do
|
for j=1,10 do
|
||||||
local room = map_matrix[i][j]
|
local room = map_matrix[i][j]
|
||||||
|
|
|
@ -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
|
|
@ -38,7 +38,7 @@ ht_create(unsigned int size)
|
||||||
}
|
}
|
||||||
|
|
||||||
static unsigned int
|
static unsigned int
|
||||||
hash(Hashtable *table, char *key)
|
hash(Hashtable *table, const char *key)
|
||||||
{
|
{
|
||||||
unsigned long int hashval = 0;
|
unsigned long int hashval = 0;
|
||||||
int i = 0;
|
int i = 0;
|
||||||
|
@ -52,7 +52,7 @@ hash(Hashtable *table, char *key)
|
||||||
}
|
}
|
||||||
|
|
||||||
static Entry*
|
static Entry*
|
||||||
entry_create(char *key, void *value)
|
entry_create(const char *key, void *value)
|
||||||
{
|
{
|
||||||
Entry *entry;
|
Entry *entry;
|
||||||
|
|
||||||
|
@ -65,7 +65,7 @@ entry_create(char *key, void *value)
|
||||||
}
|
}
|
||||||
|
|
||||||
void
|
void
|
||||||
ht_set(Hashtable *table, char *key, void *val)
|
ht_set(Hashtable *table, const char *key, void *val)
|
||||||
{
|
{
|
||||||
int hashkey = 0;
|
int hashkey = 0;
|
||||||
Entry *newEntry;
|
Entry *newEntry;
|
||||||
|
@ -106,7 +106,7 @@ ht_set(Hashtable *table, char *key, void *val)
|
||||||
}
|
}
|
||||||
|
|
||||||
void*
|
void*
|
||||||
ht_get(Hashtable *table, char *key)
|
ht_get(Hashtable *table, const char *key)
|
||||||
{
|
{
|
||||||
int hashkey = 0;
|
int hashkey = 0;
|
||||||
Entry *entry;
|
Entry *entry;
|
||||||
|
@ -149,6 +149,7 @@ ht_destroy_custom(Hashtable *table, void (*destroy_value)(void *value))
|
||||||
next = entry->next;
|
next = entry->next;
|
||||||
destroy_value(entry->value);
|
destroy_value(entry->value);
|
||||||
entry->value = NULL;
|
entry->value = NULL;
|
||||||
|
free(entry->key);
|
||||||
free(entry);
|
free(entry);
|
||||||
entry = next;
|
entry = next;
|
||||||
}
|
}
|
||||||
|
|
|
@ -14,9 +14,9 @@ typedef struct table_t {
|
||||||
|
|
||||||
Hashtable* ht_create(unsigned int size);
|
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*);
|
void ht_destroy(Hashtable*);
|
||||||
|
|
||||||
|
|
|
@ -62,7 +62,7 @@ void map_add_decoration(Map *map, Position *tile_pos, MapTile *tile)
|
||||||
}
|
}
|
||||||
|
|
||||||
Texture*
|
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;
|
Texture *t;
|
||||||
|
|
||||||
|
@ -218,7 +218,7 @@ void map_destroy(Map *map)
|
||||||
texture_destroy(linkedlist_pop(&map->textures));
|
texture_destroy(linkedlist_pop(&map->textures));
|
||||||
}
|
}
|
||||||
while (map->monsters != NULL) {
|
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);
|
ht_destroy_custom(map->monsterTextures, (void (*)(void*)) texture_destroy);
|
||||||
timer_destroy(map->renderTimer);
|
timer_destroy(map->renderTimer);
|
||||||
|
|
|
@ -46,7 +46,7 @@ void map_add_tile(Map *map, Position *tile_pos, MapTile*);
|
||||||
|
|
||||||
void map_add_decoration(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*);
|
void map_add_monster(Map*, Monster*);
|
||||||
|
|
||||||
|
|
|
@ -144,6 +144,54 @@ int l_add_decoration(lua_State *L)
|
||||||
return 0;
|
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)
|
Map* map_lua_generator_run(SDL_Renderer *renderer)
|
||||||
{
|
{
|
||||||
int status, result;
|
int status, result;
|
||||||
|
@ -177,6 +225,9 @@ Map* map_lua_generator_run(SDL_Renderer *renderer)
|
||||||
lua_pushcfunction(L, l_map_set_current_room);
|
lua_pushcfunction(L, l_map_set_current_room);
|
||||||
lua_setglobal(L, "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);
|
result = lua_pcall(L, 0, LUA_MULTRET, 0);
|
||||||
if (result) {
|
if (result) {
|
||||||
fprintf(stderr, "[!!] Failed to run script: %s\n",
|
fprintf(stderr, "[!!] Failed to run script: %s\n",
|
||||||
|
|
|
@ -6,12 +6,15 @@ monster_create()
|
||||||
{
|
{
|
||||||
Monster *m = ec_malloc(sizeof(Monster));
|
Monster *m = ec_malloc(sizeof(Monster));
|
||||||
m->sprite = sprite_create();
|
m->sprite = sprite_create();
|
||||||
|
m->clip = (SDL_Rect) { 0, 0, 16, 16 };
|
||||||
return m;
|
return m;
|
||||||
}
|
}
|
||||||
|
|
||||||
void
|
void
|
||||||
monster_render(Monster *m, Camera *cam)
|
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);
|
sprite_render(m->sprite, cam);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -8,6 +8,7 @@
|
||||||
typedef struct {
|
typedef struct {
|
||||||
Sprite *sprite;
|
Sprite *sprite;
|
||||||
Stats stats;
|
Stats stats;
|
||||||
|
SDL_Rect clip;
|
||||||
} Monster;
|
} Monster;
|
||||||
|
|
||||||
Monster* monster_create();
|
Monster* monster_create();
|
||||||
|
|
|
@ -65,15 +65,19 @@ void handle_player_input(Sprite *sprite, RoomMatrix *matrix, SDL_Event *event)
|
||||||
if (event->type == SDL_KEYDOWN) {
|
if (event->type == SDL_KEYDOWN) {
|
||||||
switch (event->key.keysym.sym) {
|
switch (event->key.keysym.sym) {
|
||||||
case SDLK_LEFT:
|
case SDLK_LEFT:
|
||||||
|
case SDLK_h:
|
||||||
move_left(sprite, matrix);
|
move_left(sprite, matrix);
|
||||||
break;
|
break;
|
||||||
case SDLK_RIGHT:
|
case SDLK_RIGHT:
|
||||||
|
case SDLK_l:
|
||||||
move_right(sprite, matrix);
|
move_right(sprite, matrix);
|
||||||
break;
|
break;
|
||||||
case SDLK_UP:
|
case SDLK_UP:
|
||||||
|
case SDLK_k:
|
||||||
move_up(sprite, matrix);
|
move_up(sprite, matrix);
|
||||||
break;
|
break;
|
||||||
case SDLK_DOWN:
|
case SDLK_DOWN:
|
||||||
|
case SDLK_j:
|
||||||
move_down(sprite, matrix);
|
move_down(sprite, matrix);
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
|
@ -28,3 +28,25 @@ Position position_to_room_coords(Position *src)
|
||||||
|
|
||||||
return pos;
|
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;
|
||||||
|
}
|
||||||
|
|
|
@ -1,6 +1,8 @@
|
||||||
#ifndef POSITION_H_
|
#ifndef POSITION_H_
|
||||||
#define POSITION_H_
|
#define POSITION_H_
|
||||||
|
|
||||||
|
#include <stdbool.h>
|
||||||
|
|
||||||
typedef struct {
|
typedef struct {
|
||||||
int x;
|
int x;
|
||||||
int y;
|
int y;
|
||||||
|
@ -10,4 +12,6 @@ Position position_to_matrix_coords(Position*);
|
||||||
|
|
||||||
Position position_to_room_coords(Position*);
|
Position position_to_room_coords(Position*);
|
||||||
|
|
||||||
|
bool position_in_room(Position *pos, Position *roomPos);
|
||||||
|
|
||||||
#endif // POSITION_H_
|
#endif // POSITION_H_
|
||||||
|
|
|
@ -12,8 +12,10 @@ RoomMatrix* roommatrix_create()
|
||||||
|
|
||||||
void roommatrix_populate_from_map(RoomMatrix *rm, Map *m)
|
void roommatrix_populate_from_map(RoomMatrix *rm, Map *m)
|
||||||
{
|
{
|
||||||
int i, j;
|
int i, j, monster_count;
|
||||||
|
Position monster_matrix_pos;
|
||||||
Room *r;
|
Room *r;
|
||||||
|
Monster *monster;
|
||||||
|
|
||||||
roommatrix_reset(rm);
|
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
|
static int
|
||||||
|
|
21
src/sprite.c
21
src/sprite.c
|
@ -7,7 +7,7 @@ Sprite* sprite_create_default()
|
||||||
Position pos = { 0, 0 };
|
Position pos = { 0, 0 };
|
||||||
|
|
||||||
Sprite *s = ec_malloc(sizeof(Sprite));
|
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();
|
s->renderTimer = timer_create();
|
||||||
|
|
||||||
|
@ -50,20 +50,19 @@ sprite_set_texture(Sprite *s, Texture *t, int index)
|
||||||
|
|
||||||
void sprite_render(Sprite *s, Camera *cam)
|
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))
|
if (timer_get_ticks(s->renderTimer) > 300) {
|
||||||
timer_start(s->renderTimer);
|
timer_stop(s->renderTimer);
|
||||||
|
timer_start(s->renderTimer);
|
||||||
if (timer_get_ticks(s->renderTimer) > 300) {
|
s->texture_index = s->texture_index == 0 ? 1 : 0;
|
||||||
timer_stop(s->renderTimer);
|
}
|
||||||
timer_start(s->renderTimer);
|
|
||||||
if (s->textures[1])
|
|
||||||
t_index = t_index == 0 ? 1 : 0;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
Position cameraPos = camera_to_camera_position(cam, &s->pos);
|
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)
|
void sprite_destroy(Sprite *sprite)
|
||||||
|
|
|
@ -14,6 +14,7 @@ typedef struct Sprite_t {
|
||||||
bool destroyTextures;
|
bool destroyTextures;
|
||||||
Position pos;
|
Position pos;
|
||||||
Timer *renderTimer;
|
Timer *renderTimer;
|
||||||
|
unsigned int texture_index;
|
||||||
void (*handle_event)(struct Sprite_t*, RoomMatrix*, SDL_Event*);
|
void (*handle_event)(struct Sprite_t*, RoomMatrix*, SDL_Event*);
|
||||||
} Sprite;
|
} Sprite;
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue