Skip to content

Commit d553e8d

Browse files
committed
added wall
Signed-off-by: Piotr Danecki <piotr.a.danecki@gmail.com>
1 parent 7231a7b commit d553e8d

12 files changed

Lines changed: 211 additions & 50 deletions

File tree

alchemist/axe.cpp

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
#include "axe.h"
2+
#include <cstddef>
23
#include <cstdio>
34
#include "../player.h"
45
#include "../world.h"
@@ -62,6 +63,24 @@ bool Axe::use(Player * player)
6263
// TODO break when used too much
6364
}
6465
}
66+
67+
Object * o = world_table[player->map_y][player->map_x]->objects[i];
68+
if (o && o->type == OBJECT_wall)
69+
{
70+
int x,y;
71+
o->get_posittion(&x, &y);
72+
if (player->x == x && player->y == y)
73+
{
74+
Element * el = new Element(o->base);
75+
el->set_posittion(player->x, player->y);
76+
set_item_at_ppos(el, player);
77+
78+
free(o);
79+
o=NULL;
80+
world_table[player->map_y][player->map_x]->objects[i]=NULL;
81+
return true;
82+
}
83+
}
6584
}
6685
return false;
6786
}

alchemist/elements.cpp

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
#include <SDL2/SDL_render.h>
12
#include <cstdio>
23
#include <cstdlib>
34

@@ -39,6 +40,11 @@ const char * items_name[]=
3940
"Stick",
4041
};
4142

43+
const char * object_names[]=
44+
{
45+
"wall"
46+
};
47+
4248
const char * food_name[]=
4349
{
4450
"Pumpkin",
@@ -158,6 +164,11 @@ void BaseElement::show(bool details)
158164
if (edible) edible->show();
159165
}
160166

167+
SDL_Texture * Object::get_texture()
168+
{
169+
return object_textures[type];
170+
}
171+
161172
SDL_Texture * Being::get_texture()
162173
{
163174
return being_textures[type];

alchemist/elements.h

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,34 @@ class InventoryElement
119119
void get_posittion(int *_x, int *_y) { *_x=x; *_y=y; }
120120
};
121121

122+
enum object_types
123+
{
124+
OBJECT_wall,
125+
};
126+
127+
extern const char * object_names[];
128+
129+
#define OBJECTS 1
130+
131+
class Object : public InventoryElement
132+
{
133+
public:
134+
BaseElement * base;
135+
enum object_types type;
136+
void * information;
137+
Form get_form() {return Form_solid; }
138+
const char * get_form_name() { return Form_name[Form_solid]; }
139+
const char * get_name() {
140+
return object_names[type];
141+
}
142+
void show() {
143+
printf("Object type: %s", get_name());
144+
base->show();
145+
}
146+
#ifndef STUB_SDL
147+
SDL_Texture * get_texture();
148+
#endif
149+
};
122150

123151
class Element : public InventoryElement
124152
{
@@ -131,6 +159,10 @@ class Element : public InventoryElement
131159
unsigned int width;
132160
unsigned int height;
133161
unsigned int volume; //lenght*width*height
162+
BaseElement * get_base()
163+
{
164+
return base;
165+
}
134166
Edible * get_edible()
135167
{
136168
return base->edible;

main.cpp

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -399,6 +399,13 @@ void player_interact(int key)
399399
if (a)
400400
a->show();
401401
}
402+
Object ** o_at_ppos = get_object_at_ppos(&player);
403+
if (o_at_ppos)
404+
{
405+
Object * o = *o_at_ppos;
406+
if (o)
407+
o->show();
408+
}
402409
}
403410
break;
404411

@@ -688,6 +695,18 @@ void draw()
688695
SDL_RenderCopy(renderer, a->get_texture(), NULL, &img_rect);
689696
}
690697
}
698+
// render objects
699+
for (int i = 0; i < CHUNK_SIZE*CHUNK_SIZE; i++) {
700+
Object * o = world_table[player.map_y][player.map_x]->objects[i];
701+
if (o)
702+
{
703+
int x,y;
704+
o->get_posittion(&x, &y);
705+
706+
SDL_Rect img_rect = {x * tile_dungeon_size, y * tile_dungeon_size, tile_dungeon_size, tile_dungeon_size};
707+
SDL_RenderCopy(renderer, o->get_texture(), NULL, &img_rect);
708+
}
709+
}
691710

692711
// render player
693712
SDL_Rect img_rect = {player.x * tile_dungeon_size, player.y * tile_dungeon_size, tile_dungeon_size, tile_dungeon_size};

menu.cpp

Lines changed: 96 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,14 @@
11
#include "menu.h"
2+
#include "text.h"
3+
#include "tiles.h"
24
#include "window.h"
35
#include "music.h"
46
#include "alchemist/elements.h"
57
#include "alchemist/axe.h"
68
#include "alchemist/knife.h"
79
#include "world.h"
810
#include "craft.h"
11+
#include <cstddef>
912
#include <cstdio>
1013
#include <cstdlib>
1114

@@ -23,6 +26,7 @@ Menu *menu_inventory_categories;
2326
Menu *menu_inventory;
2427
Menu *menu_crafting;
2528
Menu *menu_dev;
29+
Menu *menu_build;
2630

2731
void load(char with_player);
2832
void save(char with_player);
@@ -151,6 +155,7 @@ void create_menus()
151155
menu_main->add("Change music volume", MENU_MUSIC);
152156
menu_main->add("Cancel", MENU_CANCEL);
153157

158+
154159
menu_help = new Menu("Help 1", 9);
155160
menu_help->add("; - show item info", MENU_CANCEL);
156161
menu_help->add("f11 - resize", MENU_CANCEL);
@@ -202,8 +207,12 @@ void create_menus()
202207
menu_crafting->add("Knife blade (1 ing.)", MENU_CRAFT_KNIFE_BLADE);
203208
menu_crafting->add("Knife handle (1 ing.)", MENU_CRAFT_KNIFE_HANDLE);
204209
menu_crafting->add("Knife (2 ing.)", MENU_CRAFT_KNIFE);
210+
205211

206212
menu_crafting->add("Cancel", MENU_CANCEL);
213+
214+
menu_build = new Menu("build", 1);
215+
menu_build->add("Wall (1 ing.)", MENU_BUILD_WALL);
207216

208217
menu_dev = new Menu("dev options", 4);
209218
menu_dev->add("axe", MENU_GET_AXE);
@@ -253,56 +262,62 @@ void Menu::go_up()
253262
int menu_interact(int key)
254263
{
255264
switch (key)
256-
{
257-
case SDLK_ESCAPE:
258-
{
259-
if (current_menu) current_menu=NULL; else current_menu=menu_main;
260-
return 1;
261-
}
262-
case SDLK_l:
263-
{
264-
if (current_menu) current_menu=NULL; else current_menu=menu_dev;
265-
return 1;
266-
}
267-
case SDLK_c:
268-
{
269-
if (!current_menu) current_menu=menu_crafting; else if (current_menu == menu_crafting) current_menu=NULL;
270-
return 1;
271-
}
265+
{
266+
case SDLK_b:
267+
{
268+
if (!current_menu) current_menu=menu_build; else if (current_menu == menu_build) current_menu=NULL;
269+
270+
return 1;
271+
}
272+
case SDLK_ESCAPE:
273+
{
274+
if (current_menu) current_menu=NULL; else current_menu=menu_main;
275+
return 1;
276+
}
277+
case SDLK_l:
278+
{
279+
if (current_menu) current_menu=NULL; else current_menu=menu_dev;
280+
return 1;
281+
}
282+
case SDLK_c:
283+
{
284+
if (!current_menu) current_menu=menu_crafting; else if (current_menu == menu_crafting) current_menu=NULL;
285+
return 1;
286+
}
272287
case SDLK_i:
273-
{
274-
player.inventory->show();
275-
if (!current_menu) current_menu=menu_inventory_categories;
276-
else if (current_menu == menu_inventory_categories) current_menu=NULL;
277-
return 1;
278-
}
279-
280-
case SDLK_DOWN:
281-
case SDLK_s:
282-
{
283-
if (current_menu) current_menu->go_down();
284-
break;
285-
}
286-
case SDLK_w:
287-
case SDLK_UP:
288-
{
289-
if (current_menu) current_menu->go_up();
290-
break;
291-
}
292-
293-
case SDLK_RETURN:
294-
case SDLK_e:
295-
{
296-
if (current_menu)
297-
{
298-
if (interact(current_menu->actions[current_menu->menu_pos]))
299-
{
300-
current_menu=NULL;
301-
return 1;
302-
}
303-
}
304-
break;
305-
}
288+
{
289+
player.inventory->show();
290+
if (!current_menu) current_menu=menu_inventory_categories;
291+
else if (current_menu == menu_inventory_categories) current_menu=NULL;
292+
return 1;
293+
}
294+
295+
case SDLK_DOWN:
296+
case SDLK_s:
297+
{
298+
if (current_menu) current_menu->go_down();
299+
break;
300+
}
301+
case SDLK_w:
302+
case SDLK_UP:
303+
{
304+
if (current_menu) current_menu->go_up();
305+
break;
306+
}
307+
308+
case SDLK_RETURN:
309+
case SDLK_e:
310+
{
311+
if (current_menu)
312+
{
313+
if (interact(current_menu->actions[current_menu->menu_pos]))
314+
{
315+
current_menu=NULL;
316+
return 1;
317+
}
318+
}
319+
break;
320+
}
306321
}
307322
return current_menu ? 1 : 0;
308323
}
@@ -332,6 +347,7 @@ int craft(menu_actions a)
332347
case MENU_CRAFT_AXE_BLADE: el = craft_axe_blade(); break;
333348
case MENU_CRAFT_AXE_HANDLE: el = craft_axe_handle(); break;
334349
case MENU_CRAFT_AXE: el = craft_axe(); break;
350+
335351
}
336352
if (el) {
337353
set_item_at_ppos(el, &player);
@@ -347,6 +363,37 @@ int interact(enum menu_actions a)
347363
if (a & MENU_ITEM) return handle_item(a & ~MENU_ITEM);
348364
switch(a)
349365
{
366+
case MENU_BUILD_WALL:
367+
{
368+
sprintf(status_line, "Starting building");
369+
if (player.hotbar[active_hotbar] && player.hotbar[active_hotbar]->get_form() == Form_solid)
370+
{
371+
Object * ob = new Object();
372+
ob->base = ((Element *)(player.hotbar[active_hotbar]))->get_base();
373+
374+
player.inventory->remove(player.hotbar[active_hotbar]);
375+
player.hotbar[active_hotbar]=NULL;
376+
377+
ob->type = OBJECT_wall;
378+
ob->set_posittion(player.x, player.y);
379+
380+
for (int i = 0; i < CHUNK_SIZE*CHUNK_SIZE; i++)
381+
{
382+
if (!world_table[player.map_y][player.map_x]->objects[i])
383+
{
384+
world_table[player.map_y][player.map_x]->objects[i] = ob;
385+
break;
386+
}
387+
}
388+
389+
status_code = 1;
390+
}
391+
else {
392+
status_code = 0;
393+
}
394+
break;
395+
}
396+
350397
case MENU_GET_AXE:
351398
{
352399
Element * el1=new Element(base_elements[0]);

menu.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ enum menu_actions
3131
MENU_GET_KNIFE,
3232
MENU_GET_RANDOM_ELEMENT,
3333
MENU_GET_RANDOM_EDIBLE,
34+
MENU_BUILD_WALL,
3435
//must be the last
3536
MENU_ITEM=0x1000,
3637
};

texture.cpp

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ SDL_Texture * animall_textures[ANIMALS];
1414
SDL_Texture * animalr_textures[ANIMALS];
1515
SDL_Texture * plant_textures[PLANTS];
1616
SDL_Texture * grown_plant_textures[PLANTS];
17-
17+
SDL_Texture * object_textures[OBJECTS];
1818

1919
SDL_Texture* load_texture(const char * texture_name)
2020
{
@@ -129,4 +129,6 @@ void load_textures()
129129

130130
animall_textures[ANIMALID_pig] = load_texture("textures/animals/pigl.png");
131131
animalr_textures[ANIMALID_pig] = load_texture("textures/animals/pigr.png");
132+
133+
object_textures[OBJECT_wall] = load_texture("textures/objects/wall.png");
132134
}

texture.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ extern SDL_Texture * animall_textures[ANIMALS];
2727
extern SDL_Texture * animalr_textures[ANIMALS];
2828
extern SDL_Texture * plant_textures[PLANTS];
2929
extern SDL_Texture * grown_plant_textures[PLANTS];
30+
extern SDL_Texture * object_textures[OBJECTS];
3031
extern SDL_Texture * up_mask;
3132
extern SDL_Texture * down_mask;
3233
extern struct textures Texture;

textures/objects/wall.png

2.36 KB
Loading

tiles.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@ typedef struct {
4747
Being * beings[CHUNK_SIZE*CHUNK_SIZE];
4848
Plant * plants[CHUNK_SIZE*CHUNK_SIZE];
4949
Animal * animals[CHUNK_SIZE*CHUNK_SIZE];
50+
Object * objects[CHUNK_SIZE*CHUNK_SIZE];
5051
} chunk;
5152

5253

0 commit comments

Comments
 (0)