Implementation of keys. Completely untested

This commit is contained in:
Linus Probert 2019-03-18 11:39:26 +01:00
parent 1065216b67
commit 59a1e81c6e
7 changed files with 75 additions and 2 deletions

View File

@ -18,6 +18,7 @@
#include <SDL.h>
#include <stdlib.h>
#include <assert.h>
#include "item_builder.h"
#include "texture.h"
@ -28,6 +29,7 @@
#include "texturecache.h"
#include "sprite.h"
#include "sprite_util.h"
#include "map.h"
static ItemBuilder *builder = NULL;
@ -150,6 +152,44 @@ item_builder_build_treasure(Treasure type, double goldAmt)
return item;
}
static void
pickup_silver_key(Item *item, Player *player)
{
UNUSED(item);
player->equipment.keys &= LOCK_SILVER;
}
static void
pickup_gold_key(Item *item, Player *player)
{
UNUSED(item);
player->equipment.keys &= LOCK_GOLD;
}
Item *
item_builder_build_key(unsigned int type)
{
char label[20];
SDL_Rect clip = CLIP16(0, 0);
Item *item;
switch (type) {
case 1:
m_sprintf(label, 20, "a silver key");
item = create_item("Items/Key.png", NULL, clip, &pickup_silver_key);
break;
case 2:
m_sprintf(label, 20, "a gold key");
item = create_item("Items/Key.png", NULL, clip, &pickup_gold_key);
clip.x = 16;
break;
default:
fatal("Bad keytype provided");
}
m_strcpy(item->label, 20, label);
return item;
}
static Item *
create_treasure(int current_level)
{

View File

@ -56,6 +56,9 @@ item_builder_build_container(const char *path0, const char *path1, SDL_Rect clip
Item *
item_builder_build_treasure(Treasure type, double goldAmt);
Item *
item_builder_build_key(unsigned int type);
void
item_builder_close(void);

View File

@ -640,8 +640,7 @@ resetGame(void)
info("Building new map");
gMap = map_lua_generator_run(cLevel, mode, gPlayer, gRenderer);
gPlayer->sprite->pos = (Position) {
TILE_DIMENSION, TILE_DIMENSION };
player_reset_on_levelchange(gPlayer);
map_set_current_room(gMap, &gPlayer->sprite->pos);
camera_follow_position(gCamera, &gPlayer->sprite->pos);

View File

@ -26,6 +26,7 @@
#include "particle_engine.h"
#include "update_data.h"
#include "trap.h"
#include "mixer.h"
static Room*
create_room(void)
@ -450,6 +451,19 @@ map_trigger_tile_fall(MapTile *tile)
tile->lethal = true;
}
bool
map_open_door(MapTile *tile, Player *player)
{
if (tile->lockType == LOCK_NONE || tile->lockType & player->equipment.keys) {
// Open the door
mixer_play_effect(DOOR_OPEN);
tile->sprite->texture_index = 1;
tile->collider = false;
return true;
}
return false;
}
void
map_destroy(Map *map)
{

View File

@ -128,6 +128,9 @@ map_set_current_room(Map*, Position*);
void
map_trigger_tile_fall(MapTile *tile);
bool
map_open_door(MapTile *tile, Player *player);
void
map_destroy(Map*);

View File

@ -240,6 +240,7 @@ has_collided(Player *player, RoomMatrix *matrix, Vector2d direction)
if (space->monster) {
on_monster_collision(player, space->monster, matrix, direction);
} else if (space->door) {
// Open the door
mixer_play_effect(DOOR_OPEN);
space->door->sprite->texture_index = 1;
space->door->collider = false;
@ -521,6 +522,7 @@ player_create(class_t class, Camera *cam)
player->animationTimer = _timer_create();
player->swordAnimation = animation_create(5);
player->equipment.hasArtifacts = false;
player->equipment.keys = 0;
player->stateData.shopOwnerKiller = false;
build_sword_animation(player, cam->renderer);
@ -574,6 +576,14 @@ player_create(class_t class, Camera *cam)
return player;
}
void
player_reset_on_levelchange(Player *player)
{
player->sprite->pos = (Position) {
TILE_DIMENSION, TILE_DIMENSION };
player->equipment.keys = 0;
}
ExperienceData player_get_xp_data(Player *p)
{
ExperienceData data;

View File

@ -60,6 +60,7 @@ typedef struct ArtifactData {
typedef struct PlayerEquipment {
ArtifactData artifacts[LAST_ARTIFACT_EFFECT];
bool hasArtifacts;
Uint32 keys;
} PlayerEquipment;
typedef struct PlayerStateData {
@ -88,6 +89,9 @@ typedef struct Player {
Player*
player_create(class_t, Camera*);
void
player_reset_on_levelchange(Player *player);
ExperienceData
player_get_xp_data(Player*);