diff --git a/CMakeLists.txt b/CMakeLists.txt index ffb78e2..6d75e12 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -264,6 +264,7 @@ if (NOT DEBUG_BUILD) "menumapgen.lua" "monstergen.lua" "trapgen.lua" + "chestgen.lua" "pitlayouts.dat" WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}/data ) diff --git a/assets/Sounds/FX/chest_open.wav b/assets/Sounds/FX/chest_open.wav new file mode 100644 index 0000000..485efc5 Binary files /dev/null and b/assets/Sounds/FX/chest_open.wav differ diff --git a/data/chestgen.lua b/data/chestgen.lua new file mode 100644 index 0000000..6f53c2c --- /dev/null +++ b/data/chestgen.lua @@ -0,0 +1,63 @@ +local room_builder = require "maproombuilder" +local module = {} +local random = math.random + +local textures = { + "Items/Chest0.png", + "Items/Chest1.png" +} + +local chests = {} +for i=0,1 do + table.insert(chests, { + textures[1], + textures[2], + 0, + i * 16 + }); + table.insert(chests, { + textures[1], + textures[2], + 16, + i * 16 + }); +end + +local function repack(data) + return { + texturePath1 = data[1], + texturePath2 = data[2], + clipX = data[3], + clipY = data[4] + } +end + +function module.add_chests_to_room(room) + if room.type == "coridoor" then + return + end + + local count = random(3) - 1 + local i = 0 + while i < count do + local rx = random(13) + 1 + local ry = random(9) + 1 + if room_builder.is_tile_avilable(room, rx, ry) then + room.chests[rx][ry] = chests[random(#chests)] + i = i + 1 + end + end +end + +function module.load_chests(map, chests) + for i=0,15 do + for j=0,11 do + chest = chests[i][j] + if chest then + add_chest(map, i, j, CURRENT_LEVEL, repack(chest)) + end + end + end +end + +return module diff --git a/data/mapgen.lua b/data/mapgen.lua index ecb7b2e..672ebe9 100644 --- a/data/mapgen.lua +++ b/data/mapgen.lua @@ -1,6 +1,7 @@ local room_builder = require "maproombuilder" local monster_gen = require "monstergen" local trap_gen = require "trapgen" +local chest_gen = require "chestgen" -- Setting up some functions local time = os.time @@ -117,6 +118,7 @@ local function generate_path () room_builder.build_room(room) monster_gen.add_monsters_to_room(room, i-1, j-1) trap_gen.add_traps_to_room(room, i-1, j-1) + chest_gen.add_chests_to_room(room, i-1, j-1) end end end @@ -141,6 +143,7 @@ for i=1,10 do room_builder.load_room(map, room) monster_gen.load_monsters(map, room.monsters) trap_gen.load_traps(map, room.traps) + chest_gen.load_chests(map, room.chests) end end end diff --git a/data/maproombuilder.lua b/data/maproombuilder.lua index baea866..4849cde 100644 --- a/data/maproombuilder.lua +++ b/data/maproombuilder.lua @@ -131,7 +131,7 @@ local function repack(data) end local function add_random_decor_to_room(room) - local decor_count = random(10) - 1 + local decor_count = random(8) for i=1,decor_count do x = random(11) + 1 y = random(8) + 1 @@ -452,6 +452,16 @@ function module.add_full_lighting(room) room.decor[11][9] = lightDecor.candle2 end +function module.is_tile_avilable(room, rx, ry) + return not room.chests[rx][ry] + and not room.traps[rx][ry] + and not room.monsters[rx][ry] + and not room.decor[rx][ry] + and (room.tiles[rx][ry] + and not room.tiles[rx][ry][5] + and not room.tiles[rx][ry][8]) +end + function module.create_empty_room() room = { exits = {}, @@ -466,6 +476,7 @@ function module.create_empty_room() arg = nil }, monsters = {}, + chests = {}, traps = {} } for i=0,15 do @@ -473,11 +484,13 @@ function module.create_empty_room() room.decor[i] = {} room.monsters[i] = {} room.traps[i] = {} + room.chests[i] = {} for j=0,11 do room.tiles[i][j] = nil room.decor[i][j] = nil room.monsters[i][j] = nil room.traps[i][j] = nil + room.chests[i][j] = nil end end return room diff --git a/data/monstergen.lua b/data/monstergen.lua index e6b33ad..e9c5ee3 100644 --- a/data/monstergen.lua +++ b/data/monstergen.lua @@ -1,3 +1,4 @@ +local room_builder = require "maproombuilder" local module = {} local random = math.random @@ -250,13 +251,7 @@ function module.add_monsters_to_room(room, roomx, roomy) while i < count do local rx = random(13) + 1 local ry = random(9) + 1 - if not room.decor[rx][ry] - and not room.monsters[rx][ry] - and (room.tiles[rx][ry] - and not room.tiles[rx][ry][5] - and not room.tiles[rx][ry][8]) - then - + if room_builder.is_tile_avilable(room, rx, ry) then local x = (roomx * 512) + rx * 32 local y = (roomy * 384) + ry * 32 room.monsters[rx][ry] = { diff --git a/data/trapgen.lua b/data/trapgen.lua index 67fc4a0..2948d75 100644 --- a/data/trapgen.lua +++ b/data/trapgen.lua @@ -1,3 +1,4 @@ +local room_builder = require "maproombuilder" local module = {} local random = math.random @@ -34,13 +35,7 @@ function module.add_traps_to_room(room) while i < count do local rx = random(13) + 1 local ry = random(9) + 1 - if not room.decor[rx][ry] - and not room.monsters[rx][ry] - and (room.tiles[rx][ry] - and not room.tiles[rx][ry][5] - and not room.tiles[rx][ry][8]) - then - + if room_builder.is_tile_avilable(room, rx, ry) then room.traps[rx][ry] = traps[random(#traps)] i = i + 1 end diff --git a/src/item.c b/src/item.c index 34a6abf..84ca455 100644 --- a/src/item.c +++ b/src/item.c @@ -19,6 +19,7 @@ #include #include "item.h" #include "util.h" +#include "mixer.h" Item * item_create(void) @@ -59,6 +60,7 @@ item_collected(Item *item, Player *player) if (!item->openable) { item->collected = true; } else { + mixer_play_effect(CHEST_OPEN); item->opened = true; item->sprite->texture_index = 1; } diff --git a/src/map_lua.c b/src/map_lua.c index 663652f..c643ed2 100644 --- a/src/map_lua.c +++ b/src/map_lua.c @@ -330,15 +330,15 @@ l_add_chest(lua_State *L) cr->x * MAP_ROOM_WIDTH * TILE_DIMENSION + x * TILE_DIMENSION, cr->y * MAP_ROOM_HEIGHT * TILE_DIMENSION + y * TILE_DIMENSION }; - if (get_random(2) == 0) + lua_pop(L, 4); + + if (get_random(1) == 0) linkedlist_append(&chest->items, item_builder_build_item(level, TREASURE)); if (get_random(4) == 0) linkedlist_append(&chest->items, item_builder_build_item(level, HEALTH)); if (get_random(4) == 0) linkedlist_append(&chest->items, item_builder_build_item(level, DAGGER)); - lua_pop(L, 4); - linkedlist_append(&map->items, chest); return 0; diff --git a/src/mixer.c b/src/mixer.c index e002637..84adae8 100644 --- a/src/mixer.c +++ b/src/mixer.c @@ -83,6 +83,7 @@ load_effects(void) effects[FALL1] = load_effect("Sounds/FX/fall1.wav"); effects[SLAM] = load_effect("Sounds/FX/slam.wav"); effects[MAGIC_PICKUP] = load_effect("Sounds/FX/magic_pickup.wav"); + effects[CHEST_OPEN] = load_effect("Sounds/FX/chest_open.wav"); } void diff --git a/src/mixer.h b/src/mixer.h index 637ce5c..a3c115c 100644 --- a/src/mixer.h +++ b/src/mixer.h @@ -59,6 +59,7 @@ typedef enum Fx_t { DAGGER_PICKUP, SLAM, MAGIC_PICKUP, + CHEST_OPEN, LAST_EFFECT } Fx;