Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

show start screen when the player is dead

  • Loading branch information...
commit 8876c63e7db8e6e89658f70035293c3eabfc5ac9 1 parent 60949f4
@ananthakumaran authored
View
1  src/creature.c
@@ -25,6 +25,7 @@ static Creature* Creature_create(World *world, char *name, char glyph, int color
creature->defense_value = 0;
creature->vision_radius = 0;
creature->inventory_size = inventory_size;
+ creature->alive = 1;
creature->armor = NULL;
creature->weapon = NULL;
View
1  src/creature.h
@@ -19,6 +19,7 @@ typedef struct Creature {
int attack_value;
int defense_value;
int vision_radius;
+ int alive;
Item *armor;
Item *weapon;
View
12 src/main.c
@@ -11,8 +11,6 @@
static void finish(int sig);
static void game_loop();
-static Screen *screen = NULL;
-
static void print_glyphs()
{
int i = 0;
@@ -62,8 +60,7 @@ int main()
init_pair(7, COLOR_WHITE, COLOR_BLACK);
init_pair(8, COLOR_BLACK, COLOR_WHITE);
- screen = Playscreen_create();
- game_loop();
+ game_loop(Startscreen_create());
/* print_glyphs(); */
@@ -76,7 +73,7 @@ static void finish(int sig)
exit(sig);
}
-void game_loop()
+void game_loop(Screen *screen)
{
int input;
@@ -91,7 +88,10 @@ void game_loop()
}
usleep(5000);
- if(screen->tick) screen->tick(screen);
+ if(screen && screen->tick) {
+ screen = screen->tick(screen);
+ }
+
} while(screen != NULL);
View
97 src/screen.c
@@ -53,8 +53,7 @@ static void display_messages(List *messages, int height)
m->life--;
if(m->life < 1) {
- if(m->free_msg) free(m->msg);
- free(m);
+ Message_destroy(m);
List_push(dead_messages, cur);
}
}
@@ -117,9 +116,20 @@ static void display_items(Screen *screen)
}
}
+static Screen *Screen_create(Screen_draw draw, Screen_handle_input handle_input)
+{
+ Screen *screen = malloc(sizeof(Screen));
+ die(screen, "Could not create screen");
+
+ screen->draw = draw;
+ screen->handle_input = handle_input;
+
+ return screen;
+}
+
// play screen
-void Playscreen_draw(Screen *screen)
+static void Playscreen_draw(Screen *screen)
{
int x = 0, y = 0, wx, wy, wz;
Creature *creature;
@@ -173,7 +183,7 @@ void Playscreen_draw(Screen *screen)
}
-Screen* Playscreen_handle_input(Screen *screen, int key)
+static Screen* Playscreen_handle_input(Screen *screen, int key)
{
Creature *player = screen->world->player;
@@ -222,18 +232,29 @@ Screen* Playscreen_handle_input(Screen *screen, int key)
return screen;
}
-void Playscreen_tick(Screen *screen)
+static void Playscreen_destroy(Screen *screen)
+{
+ World_destroy(screen->world);
+ Bitmap_destroy(screen->visible);
+ Screen_destroy(screen);
+}
+
+static Screen *Playscreen_tick(Screen *screen)
{
World_tick(screen->world);
+
+ if(!screen->world->player->alive) {
+ Playscreen_destroy(screen);
+ screen = Startscreen_create();
+ }
+
+ return screen;
}
Screen* Playscreen_create()
{
- Screen *screen = malloc(sizeof(Screen));
- die(screen, "Could not create screen.");
+ Screen *screen = Screen_create(Playscreen_draw, Playscreen_handle_input);
- screen->draw = Playscreen_draw;
- screen->handle_input = Playscreen_handle_input;
screen->tick = Playscreen_tick;
screen->world = World_create();
screen->visible = Bitmap_create(screen->world->width * screen->world->height * screen->world->depth);
@@ -244,7 +265,7 @@ Screen* Playscreen_create()
return screen;
}
-Screen* Inventoryscreen_handle_input(Screen *screen, int key)
+static Screen* Inventoryscreen_handle_input(Screen *screen, int key)
{
World *world = screen->world;
Creature *player = world->player;
@@ -284,7 +305,7 @@ Screen* Inventoryscreen_handle_input(Screen *screen, int key)
}
-void Inventoryscreen_draw(Screen *screen)
+static void Inventoryscreen_draw(Screen *screen)
{
World *world = screen->world;
Creature *player = world->player;
@@ -319,11 +340,8 @@ void Inventoryscreen_draw(Screen *screen)
Screen* Inventoryscreen_create(Screen *play_screen, char *action)
{
- Screen *screen = malloc(sizeof(Screen));
- die(screen, "Could not create inventory screen");
+ Screen *screen = Screen_create(Inventoryscreen_draw, Inventoryscreen_handle_input);
- screen->draw = Inventoryscreen_draw;
- screen->handle_input = Inventoryscreen_handle_input;
screen->tick = NULL;
screen->world = play_screen->world;
screen->parent = play_screen;
@@ -332,3 +350,52 @@ Screen* Inventoryscreen_create(Screen *play_screen, char *action)
return screen;
}
+
+// start screen
+static Screen* Startscreen_handle_input(Screen *screen, int key)
+{
+ switch(key) {
+ case 's':
+ Screen_destroy(screen);
+ screen = Playscreen_create();
+ break;
+ case 'q':
+ Screen_destroy(screen);
+ screen = NULL;
+ break;
+ }
+
+ return screen;
+}
+
+static void Startscreen_draw(Screen *screen)
+{
+
+ int height = 10;
+ size_t i;
+ char *banner[] = {
+ "Welcome to cave",
+ "",
+ "",
+ "Press [s] to start or [q] to quit",
+ "",
+ "-----------",
+ "",
+ ""
+ };
+
+ for(i = 0; i < (sizeof(banner) / sizeof(char *)); i++) {
+ move(height + i, 0);
+ addstr(banner[i]);
+ }
+
+}
+
+
+Screen* Startscreen_create()
+{
+ Screen *screen = Screen_create(Startscreen_draw, Startscreen_handle_input);
+ screen->tick = NULL;
+
+ return screen;
+}
View
9 src/screen.h
@@ -8,7 +8,7 @@ typedef struct Screen Screen;
typedef void (*Screen_draw)(Screen *screen);
typedef Screen* (*Screen_handle_input)(Screen *screen, int key);
-typedef void (*Screen_tick)(Screen *screen);
+typedef Screen* (*Screen_tick)(Screen *screen);
struct Screen {
Screen_draw draw;
@@ -25,13 +25,12 @@ struct Screen {
void Screen_destroy(Screen *screen);
// play screen
-void Playscreen_draw(Screen *screen);
-Screen* Playscreen_handle_input(Screen *screen, int key);
Screen* Playscreen_create();
// inventory screen
-void Inventoryscreen_draw();
-Screen* Inventoryscreen_handle_input(Screen *screen, int key);
Screen* Inventoryscreen_create(Screen *play_screen, char *action);
+// start screen
+Screen* Startscreen_create();
+
#endif
View
68 src/world.c
@@ -51,6 +51,19 @@ static void World_alloc_regions(World *world)
world->regions = regions;
}
+static void World_dealloc_regions(World *world)
+{
+ int i, j;
+ for(i = 0; i < world->width; i++) {
+ for(j = 0; j < world->height; j++) {
+ free(world->regions[i][j]);
+ }
+ free(world->regions[i]);
+ }
+
+ free(world->regions);
+}
+
static void World_fill_region(World *world, int region, int x, int y, int z)
{
@@ -224,6 +237,28 @@ static void World_add_creatures(World *world, Creature_create_fn creator, int co
}
}
+static void World_destroy_creatures(World *world)
+{
+ Creature *creature;
+
+ LIST_FOREACH(world->creatures, first, next, cur) {
+ creature = cur->value;
+ Creature_destroy(creature);
+ }
+
+ List_destroy(world->creatures);
+ List_destroy(world->dead_creatures);
+}
+
+static void World_destroy_messages(World *world)
+{
+ LIST_FOREACH(world->messages, first, next, cur) {
+ Message_destroy(cur->value);
+ }
+
+ List_destroy(world->messages);
+}
+
World *World_create()
{
World *world = malloc(sizeof(World));
@@ -282,11 +317,24 @@ void World_remove_creature(World *world, Creature *creature)
{
int rc;
- rc = List_delete(world->creatures, creature);
- die(rc != 0, "could not find the creature in the world.");
+ if(creature == world->player) {
+ creature->alive = 0;
+ } else {
+ rc = List_delete(world->creatures, creature);
+ die(rc != 0, "could not find the creature in the world.");
+
+ List_push(world->dead_creatures, creature);
+ Creature_destroy(creature);
+ }
+}
+
+static void World_destroy_items(World *world)
+{
+ LIST_FOREACH(world->items, first, next, cur) {
+ Item_destroy(cur->value);
+ }
- List_push(world->dead_creatures, creature);
- Creature_destroy(creature);
+ List_destroy(world->items);
}
void World_add_item(World *world, Item *item)
@@ -324,6 +372,11 @@ void World_tick(World *world)
void World_destroy(World *world)
{
Tile_destroy(world->tiles, world->height, world->width);
+ World_dealloc_regions(world);
+ World_destroy_creatures(world);
+ World_destroy_messages(world);
+ World_destroy_items(world);
+ Creature_destroy(world->player);
free(world);
}
@@ -452,3 +505,10 @@ void World_notify(World *world, char *message, int free_msg)
List_push(world->messages, m);
}
+
+
+void Message_destroy(Message *message)
+{
+ if(message->free_msg) free(message->msg);
+ free(message);
+}
View
1  src/world.h
@@ -63,5 +63,6 @@ void World_notify(World *world, char *message, int free_msg);
struct Creature *World_creature_at(World *world, int x, int y, int z);
struct Item *World_item_at(World *world, int x, int y, int z);
+void Message_destroy(Message *message);
#endif
Please sign in to comment.
Something went wrong with that request. Please try again.