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

View File

@ -49,9 +49,11 @@ local function generate_path ()
local direction = 0
local lastDirection = 0
local coridoor_count = 0
local shopLevel = CURRENT_LEVEL % 4 == 0
local bossLevel = CURRENT_LEVEL % 5 == 0
if QUICK_MODE then
bossLevel = CURRENT_LEVEL % 3 == 0
shopLevel = CURRENT_LEVEL % 2 == 0
end
local coverage = 8 + CURRENT_LEVEL
if bossLevel or CURRENT_LEVEL == 1 then
@ -59,6 +61,11 @@ local function generate_path ()
end
if ARCADE_MODE then
coverage = 40
shopLevel = true
end
if shopLevel then
coverage = coverage + 1
end
-- Create the first room
@ -114,17 +121,24 @@ local function generate_path ()
local roomCount = 0
local bossAdded = false
local shopAdded = false
-- Build all the rooms
for i=1,10 do
for j=1,10 do
room = map_matrix[i][j]
if room then
if roomCount > 3 and shopLevel and not shopAdded then
room.type = "shop"
shopAdded = true
end
roomCount = roomCount + 1
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)
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)
trap_gen.add_traps_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
bossAdded = true
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
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 crumbling = (CURRENT_LEVEL > 3 or QUICK_MODE) and random(8) == 1
@ -429,9 +437,11 @@ function module.create_empty_room()
return room
end
function module.build_room(room)
function module.build_room(room, roomx, roomy)
if room.type == "coridoor" then
build_coridoor_room(room)
elseif room.type == "shop" then
build_shop_room(room, roomx, roomy)
else
build_normal_room(room)
end

View File

@ -9,7 +9,7 @@ room_builder.load_textures(map)
set_current_room(map, 0, 0)
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)
monster_gen.add_monsters_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 "player.h"
#include "random.h"
#include "sprite_util.h"
static void
artifact_set_effect(Artifact *a, MagicalEffect effect)
@ -91,6 +92,20 @@ static int RogueArtifacts[] = {
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_create_random(Player *p, Uint8 level)
{
@ -110,25 +125,22 @@ artifact_create_random(Player *p, Uint8 level)
Artifact *a = artifact_create(artifactPool[option]);
a->level = level;
if (level > 1)
add_level_sprite(a);
return a;
}
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_load_text_texture(sprite, "GUI/SDS_8x8.ttf", 0, 8, 1);
char priceLabel[9];
m_sprintf(priceLabel, 9, "$%d", price);
texture_load_from_text(sprite->textures[0],
priceLabel,
C_YELLOW,
C_BLACK,
renderer);
sprite->dim = sprite->textures[0]->dim;
Sprite *sprite = sprite_util_create_text_sprite("GUI/SDS_8x8.ttf",
8,
C_YELLOW,
C_BLACK,
"$%u",
price);
sprite->pos = a->sprite->pos;
a->price = price;
a->priceSprite = sprite;
}
@ -196,6 +208,7 @@ artifact_create(MagicalEffect effect)
Artifact *a = ec_malloc(sizeof(Artifact));
a->sprite = artifact_sprite_for(effect);
a->priceSprite = NULL;
a->levelSprite = NULL;
a->sprite->dim = GAME_DIMENSION;
a->collected = false;
a->level = 1;
@ -220,8 +233,14 @@ artifact_render(Artifact *a, Camera *cam)
pos.x += 4;
pos.y += 4;
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);
}
if (a->levelSprite) {
a->levelSprite->pos = a->sprite->pos;
sprite_render(a->levelSprite, cam);
}
}
void
@ -230,5 +249,7 @@ artifact_destroy(Artifact *a)
sprite_destroy(a->sprite);
if (a->priceSprite)
sprite_destroy(a->priceSprite);
if (a->levelSprite)
sprite_destroy(a->levelSprite);
free(a);
}

View File

@ -41,6 +41,7 @@ typedef struct ArtifactInfo {
typedef struct Artifact {
Sprite *sprite;
Sprite *priceSprite;
Sprite *levelSprite;
MagicalEffect effect;
ArtifactInfo info;
bool collected;
@ -55,7 +56,7 @@ Artifact *
artifact_create_random(Player*, Uint8 level);
void
artifact_add_price(Artifact*, unsigned int price, SDL_Renderer *renderer);
artifact_add_price(Artifact*, unsigned int price);
Artifact *
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;
}
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
pickup_gold(Item *item, Player *player)
{

View File

@ -350,6 +350,9 @@ startGame(void)
if (gPlayer)
player_destroy(gPlayer);
gPlayer = player_create(playerClass, gCamera);
#ifdef DEBUG
gPlayer->gold = 400;
#endif
mixer_play_music(GAME_MUSIC0 + get_random(2));
resetGame();
skillbar_reset(gSkillBar);
@ -636,7 +639,7 @@ resetGame(void)
mode = ARCADE;
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) {
TILE_DIMENSION, TILE_DIMENSION };

View File

@ -37,6 +37,7 @@
#include "item_builder.h"
#include "random.h"
#include "bh_random.h"
#include "artifact.h"
static
lua_State* load_lua_state(void)
@ -94,6 +95,18 @@ SDL_Renderer* luaL_checksdlrenderer(lua_State *L)
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
l_map_set_current_room_modifier(lua_State *L)
{
@ -492,8 +505,29 @@ l_get_random(lua_State *L)
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*
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;
@ -515,6 +549,12 @@ generate_map(unsigned int level, const char *file, GameMode gameMode, SDL_Render
lua_pushlightuserdata(L, 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_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)
{
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";
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 "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);
#endif // MAP_LUA_H_