Shops implemented

Only need to add the shopkeeper now
This commit is contained in:
Linus Probert 2019-03-07 11:09:14 +01:00
parent 6009aa17f1
commit 7e1dffb45e
11 changed files with 189 additions and 50 deletions

View File

@ -153,7 +153,7 @@ function module.load_textures(map, wall_xoffset, wall_yoffset)
fences = { fences = {
topleft = { t_fence, nil, 0, yo, true }, topleft = { t_fence, nil, 0, yo, true },
top = { t_fence, nil, 16, yo, true }, top = { t_fence, nil, 16, yo, true },
single = { t_fence, nil, 16, yo + 16, true }, single = { t_fence, nil, 0, yo + 16, true },
topright = { t_fence, nil, 32, yo, true }, topright = { t_fence, nil, 32, yo, true },
left = { t_fence, nil, 0, yo + 16, true }, left = { t_fence, nil, 0, yo + 16, true },
bottomleft = { t_fence, nil, 0, yo + 32, true }, bottomleft = { t_fence, nil, 0, yo + 32, true },
@ -167,7 +167,7 @@ function module.load_textures(map, wall_xoffset, wall_yoffset)
} }
end end
function draw_layout_to_room(room, matrix) function draw_layout_to_room(room, matrix, roomx, roomy)
-- Chose a random layout -- Chose a random layout
matrix = matrix[random(#matrix)] matrix = matrix[random(#matrix)]
for i=2,13 do for i=2,13 do
@ -178,6 +178,8 @@ function draw_layout_to_room(room, matrix)
setBlockTile(room, matrix, i, j, walls, "#", false) setBlockTile(room, matrix, i, j, walls, "#", false)
elseif matrix[i][j] == "f" then elseif matrix[i][j] == "f" then
setBlockTile(room, matrix, i, j, fences, "f", true) setBlockTile(room, matrix, i, j, fences, "f", true)
elseif matrix[i][j] == "a" then
create_shop_artifact(map, (roomx*512) + i*32, (roomy * 384) + j*32)
end end
end end
end end
@ -201,4 +203,9 @@ function module.add_pits_to_room(room)
return true return true
end end
function module.add_shop_layout(room, roomx, roomy)
draw_layout_to_room(room, readLayoutFile("shoplayouts.dat"), roomx, roomy)
return true
end
return module return module

View File

@ -49,9 +49,11 @@ local function generate_path ()
local direction = 0 local direction = 0
local lastDirection = 0 local lastDirection = 0
local coridoor_count = 0 local coridoor_count = 0
local shopLevel = CURRENT_LEVEL % 4 == 0
local bossLevel = CURRENT_LEVEL % 5 == 0 local bossLevel = CURRENT_LEVEL % 5 == 0
if QUICK_MODE then if QUICK_MODE then
bossLevel = CURRENT_LEVEL % 3 == 0 bossLevel = CURRENT_LEVEL % 3 == 0
shopLevel = CURRENT_LEVEL % 2 == 0
end end
local coverage = 8 + CURRENT_LEVEL local coverage = 8 + CURRENT_LEVEL
if bossLevel or CURRENT_LEVEL == 1 then if bossLevel or CURRENT_LEVEL == 1 then
@ -59,6 +61,11 @@ local function generate_path ()
end end
if ARCADE_MODE then if ARCADE_MODE then
coverage = 40 coverage = 40
shopLevel = true
end
if shopLevel then
coverage = coverage + 1
end end
-- Create the first room -- Create the first room
@ -114,17 +121,24 @@ local function generate_path ()
local roomCount = 0 local roomCount = 0
local bossAdded = false local bossAdded = false
local shopAdded = false
-- Build all the rooms -- Build all the rooms
for i=1,10 do for i=1,10 do
for j=1,10 do for j=1,10 do
room = map_matrix[i][j] room = map_matrix[i][j]
if room then if room then
if roomCount > 3 and shopLevel and not shopAdded then
room.type = "shop"
shopAdded = true
end
roomCount = roomCount + 1 roomCount = roomCount + 1
room_builder.build_room(room) room_builder.build_room(room, i-1, j-1)
if room.type ~= "shop" then
monster_gen.add_monsters_to_room(room, i-1, j-1) monster_gen.add_monsters_to_room(room, i-1, j-1)
trap_gen.add_traps_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) chest_gen.add_chests_to_room(room, i-1, j-1)
end
if roomCount > 3 and bossLevel and not bossAdded then if roomCount > 3 and bossLevel and not bossAdded then
bossAdded = true bossAdded = true
monster_gen.add_boss_to_room(room, i-1, j-1) monster_gen.add_boss_to_room(room, i-1, j-1)

View File

@ -345,6 +345,14 @@ local function add_level_exit(room)
end end
end end
local function build_shop_room(room, roomx, roomy)
add_tiles_to_room(room, false)
add_walls_to_room(room)
add_exits_to_room(room)
layoutparser.add_shop_layout(room, roomx, roomy)
return room
end
local function build_normal_room(room) local function build_normal_room(room)
local crumbling = (CURRENT_LEVEL > 3 or QUICK_MODE) and random(8) == 1 local crumbling = (CURRENT_LEVEL > 3 or QUICK_MODE) and random(8) == 1
@ -429,9 +437,11 @@ function module.create_empty_room()
return room return room
end end
function module.build_room(room) function module.build_room(room, roomx, roomy)
if room.type == "coridoor" then if room.type == "coridoor" then
build_coridoor_room(room) build_coridoor_room(room)
elseif room.type == "shop" then
build_shop_room(room, roomx, roomy)
else else
build_normal_room(room) build_normal_room(room)
end end

View File

@ -9,7 +9,7 @@ room_builder.load_textures(map)
set_current_room(map, 0, 0) set_current_room(map, 0, 0)
local room = room_builder.create_empty_room() local room = room_builder.create_empty_room()
room_builder.build_room(room) room_builder.build_room(room, 0, 0)
room_builder.add_full_lighting(room) room_builder.add_full_lighting(room)
monster_gen.add_monsters_to_room(room, 0, 0) monster_gen.add_monsters_to_room(room, 0, 0)
trap_gen.add_traps_to_room(room, 0, 0) trap_gen.add_traps_to_room(room, 0, 0)

64
data/shoplayouts.dat Normal file
View File

@ -0,0 +1,64 @@
++++++++++++++++
++++++++++++++++
++##ffffffff##++
++##--------##++
++f---a--a---f++
++-----------f++
++-----------f++
++f---a--a---f++
++##--------##++
++##ffffffff##++
++++++++++++++++
++++++++++++++++
++++++++++++++++
++++++++++++++++
++ffff----ffff++
++fppf-a--fppf++
++ffff----ffff++
++------a-----++
++------------++
++ffff-a--ffff++
++fppf----fppf++
++ffff--a-ffff++
++++++++++++++++
++++++++++++++++
++++++++++++++++
++++++++++++++++
++############++
++#----------#++
++#-########-#++
++--#aa--aa#-#++
++--#--------#++
++#-########-#++
++#----------#++
++############++
++++++++++++++++
++++++++++++++++
++++++++++++++++
++++++++++++++++
++--pppppppp--++
++--pffffffp--++
++--pf---afp--++
++-------afp--++
++-------afp--++
++--pf---afp--++
++--pffffffp--++
++--pppppppp--++
++++++++++++++++
++++++++++++++++
++++++++++++++++
++++++++++++++++
++pppppppppppp++
++ppffffffffpp++
++fffa----afff++
++------------++
++------------++
++fffa----afff++
++ppffffffffpp++
++pppppppppppp++
++++++++++++++++
++++++++++++++++

View File

@ -22,6 +22,7 @@
#include "particle_engine.h" #include "particle_engine.h"
#include "player.h" #include "player.h"
#include "random.h" #include "random.h"
#include "sprite_util.h"
static void static void
artifact_set_effect(Artifact *a, MagicalEffect effect) artifact_set_effect(Artifact *a, MagicalEffect effect)
@ -91,6 +92,20 @@ static int RogueArtifacts[] = {
PHASE_IMPROVEMENT // 7 PHASE_IMPROVEMENT // 7
}; };
static void
add_level_sprite(Artifact *a)
{
Sprite *sprite = sprite_util_create_text_sprite("GUI/SDS_8x8.ttf",
8,
C_BLUE,
C_BLACK,
"%u",
a->level);
sprite->pos = a->sprite->pos;
sprite->offset = POS(32 - sprite->dim.width, 32 - sprite->dim.height);
a->levelSprite = sprite;
}
Artifact * Artifact *
artifact_create_random(Player *p, Uint8 level) artifact_create_random(Player *p, Uint8 level)
{ {
@ -110,25 +125,22 @@ artifact_create_random(Player *p, Uint8 level)
Artifact *a = artifact_create(artifactPool[option]); Artifact *a = artifact_create(artifactPool[option]);
a->level = level; a->level = level;
if (level > 1)
add_level_sprite(a);
return a; return a;
} }
void void
artifact_add_price(Artifact *a, unsigned int price, SDL_Renderer *renderer) artifact_add_price(Artifact *a, unsigned int price)
{ {
Sprite *sprite = sprite_create(); Sprite *sprite = sprite_util_create_text_sprite("GUI/SDS_8x8.ttf",
sprite_load_text_texture(sprite, "GUI/SDS_8x8.ttf", 0, 8, 1); 8,
char priceLabel[9];
m_sprintf(priceLabel, 9, "$%d", price);
texture_load_from_text(sprite->textures[0],
priceLabel,
C_YELLOW, C_YELLOW,
C_BLACK, C_BLACK,
renderer); "$%u",
price);
sprite->dim = sprite->textures[0]->dim; sprite->pos = a->sprite->pos;
a->price = price; a->price = price;
a->priceSprite = sprite; a->priceSprite = sprite;
} }
@ -196,6 +208,7 @@ artifact_create(MagicalEffect effect)
Artifact *a = ec_malloc(sizeof(Artifact)); Artifact *a = ec_malloc(sizeof(Artifact));
a->sprite = artifact_sprite_for(effect); a->sprite = artifact_sprite_for(effect);
a->priceSprite = NULL; a->priceSprite = NULL;
a->levelSprite = NULL;
a->sprite->dim = GAME_DIMENSION; a->sprite->dim = GAME_DIMENSION;
a->collected = false; a->collected = false;
a->level = 1; a->level = 1;
@ -220,8 +233,14 @@ artifact_render(Artifact *a, Camera *cam)
pos.x += 4; pos.x += 4;
pos.y += 4; pos.y += 4;
particle_engine_sparkle(pos, DIM(24, 24), C_PURPLE, false); particle_engine_sparkle(pos, DIM(24, 24), C_PURPLE, false);
if (a->priceSprite) if (a->priceSprite) {
a->priceSprite->pos = a->sprite->pos;
sprite_render(a->priceSprite, cam); sprite_render(a->priceSprite, cam);
}
if (a->levelSprite) {
a->levelSprite->pos = a->sprite->pos;
sprite_render(a->levelSprite, cam);
}
} }
void void
@ -230,5 +249,7 @@ artifact_destroy(Artifact *a)
sprite_destroy(a->sprite); sprite_destroy(a->sprite);
if (a->priceSprite) if (a->priceSprite)
sprite_destroy(a->priceSprite); sprite_destroy(a->priceSprite);
if (a->levelSprite)
sprite_destroy(a->levelSprite);
free(a); free(a);
} }

View File

@ -41,6 +41,7 @@ typedef struct ArtifactInfo {
typedef struct Artifact { typedef struct Artifact {
Sprite *sprite; Sprite *sprite;
Sprite *priceSprite; Sprite *priceSprite;
Sprite *levelSprite;
MagicalEffect effect; MagicalEffect effect;
ArtifactInfo info; ArtifactInfo info;
bool collected; bool collected;
@ -55,7 +56,7 @@ Artifact *
artifact_create_random(Player*, Uint8 level); artifact_create_random(Player*, Uint8 level);
void void
artifact_add_price(Artifact*, unsigned int price, SDL_Renderer *renderer); artifact_add_price(Artifact*, unsigned int price);
Artifact * Artifact *
artifact_create(MagicalEffect); artifact_create(MagicalEffect);

View File

@ -101,27 +101,6 @@ create_item(const char *path0, const char *path1, SDL_Rect clip, void (*cb)(Item
return item; return item;
} }
static Item *
create_priced_item(double price,
const char *path0,
const char *path1,
SDL_Rect clip,
void (*cb)(Item *, Player*))
{
Item *item = create_item(path0, path1, clip, cb);
item->price = price;
Sprite *priceSprite = sprite_util_create_text_sprite("GUI/SDS_8x8.ttf",
8,
C_YELLOW,
C_BLACK,
"$%.0f",
price);
linkedlist_append(&item->subsprites, priceSprite);
return item;
}
static void static void
pickup_gold(Item *item, Player *player) pickup_gold(Item *item, Player *player)
{ {

View File

@ -350,6 +350,9 @@ startGame(void)
if (gPlayer) if (gPlayer)
player_destroy(gPlayer); player_destroy(gPlayer);
gPlayer = player_create(playerClass, gCamera); gPlayer = player_create(playerClass, gCamera);
#ifdef DEBUG
gPlayer->gold = 400;
#endif
mixer_play_music(GAME_MUSIC0 + get_random(2)); mixer_play_music(GAME_MUSIC0 + get_random(2));
resetGame(); resetGame();
skillbar_reset(gSkillBar); skillbar_reset(gSkillBar);
@ -636,7 +639,7 @@ resetGame(void)
mode = ARCADE; mode = ARCADE;
info("Building new map"); info("Building new map");
gMap = map_lua_generator_run(cLevel, mode, gRenderer); gMap = map_lua_generator_run(cLevel, mode, gPlayer, gRenderer);
gPlayer->sprite->pos = (Position) { gPlayer->sprite->pos = (Position) {
TILE_DIMENSION, TILE_DIMENSION }; TILE_DIMENSION, TILE_DIMENSION };

View File

@ -37,6 +37,7 @@
#include "item_builder.h" #include "item_builder.h"
#include "random.h" #include "random.h"
#include "bh_random.h" #include "bh_random.h"
#include "artifact.h"
static static
lua_State* load_lua_state(void) lua_State* load_lua_state(void)
@ -94,6 +95,18 @@ SDL_Renderer* luaL_checksdlrenderer(lua_State *L)
return renderer; return renderer;
} }
static Player*
luaL_checkplayer(lua_State *L)
{
Player *player;
lua_getglobal(L, "_game_player");
if (!lua_islightuserdata(L, -1))
fatal("in luaL_checkplayer(), pointer lost in lua script");
player = lua_touserdata(L, -1);
return player;
}
static int static int
l_map_set_current_room_modifier(lua_State *L) l_map_set_current_room_modifier(lua_State *L)
{ {
@ -492,8 +505,29 @@ l_get_random(lua_State *L)
return 1; return 1;
} }
static int
l_create_shop_artifact(lua_State *L)
{
Player *player = luaL_checkplayer(L);
if (player == NULL)
return 0;
Map *map = luaL_checkmap(L, 1);
int x = (int) luaL_checkinteger(L, 2);
int y = (int) luaL_checkinteger(L, 3);
Artifact *a = artifact_create_random(player, 2);
a->sprite->pos = POS(x, y);
artifact_add_price(a, map->level * 50);
linkedlist_append(&map->artifacts, a);
return 0;
}
static Map* static Map*
generate_map(unsigned int level, const char *file, GameMode gameMode, SDL_Renderer *renderer) generate_map(unsigned int level, const char *file, GameMode gameMode, Player *player, SDL_Renderer *renderer)
{ {
int status, result; int status, result;
@ -515,6 +549,12 @@ generate_map(unsigned int level, const char *file, GameMode gameMode, SDL_Render
lua_pushlightuserdata(L, renderer); lua_pushlightuserdata(L, renderer);
lua_setglobal(L, "_sdl_renderer"); lua_setglobal(L, "_sdl_renderer");
lua_pushlightuserdata(L, player);
lua_setglobal(L, "_game_player");
lua_pushcfunction(L, l_create_shop_artifact);
lua_setglobal(L, "create_shop_artifact");
lua_pushcfunction(L, l_create_map); lua_pushcfunction(L, l_create_map);
lua_setglobal(L, "create_map"); lua_setglobal(L, "create_map");
@ -594,13 +634,13 @@ generate_map(unsigned int level, const char *file, GameMode gameMode, SDL_Render
Map* map_lua_generator_single_room__run(unsigned int level, SDL_Renderer *renderer) Map* map_lua_generator_single_room__run(unsigned int level, SDL_Renderer *renderer)
{ {
char file[] = "menumapgen.lua"; char file[] = "menumapgen.lua";
return generate_map(level, file, REGULAR, renderer); return generate_map(level, file, REGULAR, NULL, renderer);
} }
Map* map_lua_generator_run(unsigned int level, GameMode gameMode, SDL_Renderer *renderer) Map* map_lua_generator_run(unsigned int level, GameMode gameMode, Player *player, SDL_Renderer *renderer)
{ {
char file[] = "mapgen.lua"; char file[] = "mapgen.lua";
return generate_map(level, file, gameMode, renderer); return generate_map(level, file, gameMode, player, renderer);
} }

View File

@ -22,7 +22,7 @@
#include "map.h" #include "map.h"
#include "defines.h" #include "defines.h"
Map* map_lua_generator_run(unsigned int level, GameMode gameMode, SDL_Renderer *renderer); Map* map_lua_generator_run(unsigned int level, GameMode gameMode, Player *player, SDL_Renderer *renderer);
Map* map_lua_generator_single_room__run(unsigned int level, SDL_Renderer *renderer); Map* map_lua_generator_single_room__run(unsigned int level, SDL_Renderer *renderer);
#endif // MAP_LUA_H_ #endif // MAP_LUA_H_