Adding trees in all_green which you cannot pass over.
This commit is contained in:
parent
6b9a21e153
commit
51e9f6a8d5
|
@ -2,7 +2,13 @@
|
|||
{
|
||||
"name": "grass",
|
||||
"symbol": ".",
|
||||
"foreground": "black",
|
||||
"background": "green"
|
||||
"foreground": "green",
|
||||
"background": "black"
|
||||
},
|
||||
{
|
||||
"name": "tree",
|
||||
"symbol": "t",
|
||||
"foreground": "yellow",
|
||||
"background": "black"
|
||||
}
|
||||
]
|
||||
|
|
72
src/game.c
72
src/game.c
|
@ -21,6 +21,9 @@ l3tde_game_render_game_get_start_nodes (L3TDEGamePtr game,
|
|||
int max_x, int max_y, size_t *x, size_t *y);
|
||||
void
|
||||
l3tde_game_loop (L3TDEGamePtr game);
|
||||
void
|
||||
l3tde_game_move_to_pos_if_posible (L3TDEGamePtr game,
|
||||
size_t dest_x, size_t dest_y);
|
||||
|
||||
void
|
||||
l3tde_game_init (void) {
|
||||
|
@ -43,16 +46,23 @@ l3tde_game_loop (L3TDEGamePtr game) {
|
|||
l3tde_game_render_game (game);
|
||||
input = getch ();
|
||||
bool end_game = 0;
|
||||
bool is_movement = false;
|
||||
size_t dest_x, dest_y;
|
||||
switch (input) {
|
||||
case KEY_UP:
|
||||
if (game->y_pos) {
|
||||
(game->y_pos)--;
|
||||
is_movement = true;
|
||||
dest_x = game->x_pos;
|
||||
dest_y = game->y_pos - 1;
|
||||
}
|
||||
break;
|
||||
case KEY_DOWN:
|
||||
if (game->current_map
|
||||
->nodes_y_len -1
|
||||
> game->y_pos) {
|
||||
is_movement = true;
|
||||
dest_x = game->x_pos;
|
||||
dest_y = game->y_pos + 1;
|
||||
(game->y_pos)++;
|
||||
}
|
||||
break;
|
||||
|
@ -60,22 +70,40 @@ l3tde_game_loop (L3TDEGamePtr game) {
|
|||
if (game->current_map
|
||||
->nodes_x_len -1
|
||||
> game->x_pos) {
|
||||
(game->x_pos)++;
|
||||
is_movement = true;
|
||||
dest_x = game->x_pos + 1;
|
||||
dest_y = game->y_pos;
|
||||
}
|
||||
break;
|
||||
case KEY_LEFT:
|
||||
if (game->x_pos) {
|
||||
(game->x_pos)--;
|
||||
is_movement = true;
|
||||
dest_x = game->x_pos - 1;
|
||||
dest_y = game->y_pos;
|
||||
}
|
||||
break;
|
||||
case 'q':
|
||||
end_game = 1;
|
||||
|
||||
}
|
||||
if (is_movement) {
|
||||
l3tde_game_move_to_pos_if_posible (game, dest_x,
|
||||
dest_y);
|
||||
}
|
||||
if (end_game) break;
|
||||
}
|
||||
}
|
||||
|
||||
void
|
||||
l3tde_game_move_to_pos_if_posible (L3TDEGamePtr game,
|
||||
size_t dest_x, size_t dest_y) {
|
||||
if (game->current_map->nodes[dest_x][dest_y]->navigation
|
||||
& L3TDE_MAP_NODE_NAVIGATION_WALKABLE) {
|
||||
game->x_pos = dest_x;
|
||||
game->y_pos = dest_y;
|
||||
}
|
||||
}
|
||||
|
||||
void
|
||||
l3tde_game_render_game (L3TDEGamePtr game) {
|
||||
size_t start_node_x, start_node_y;
|
||||
|
@ -105,18 +133,40 @@ l3tde_game_render_game (L3TDEGamePtr game) {
|
|||
max_node_y = nodes_y_len;
|
||||
}
|
||||
int printing_x = 0;
|
||||
short pairs = 0;
|
||||
int **color_pairs = NULL;
|
||||
for (size_t i = start_node_x; i<max_node_x; i++, printing_x++) {
|
||||
int printing_y = 0;
|
||||
for (size_t j = start_node_y; j<max_node_y; j++, printing_y++) {
|
||||
short current_pair = 0;
|
||||
L3TDEMapNodePtr node = current_map->nodes[i][j];
|
||||
L3TDEMapTerrainPtr terrain = node->terrain;
|
||||
char symbol = terrain->symbol;
|
||||
init_pair(1, terrain->foreground, terrain->background);
|
||||
attron (COLOR_PAIR (1));
|
||||
for (short i = 0; i < pairs; i++) {
|
||||
if (color_pairs[i][0] == terrain->foreground &&
|
||||
color_pairs[i][1]
|
||||
== terrain->background ) {
|
||||
current_pair = i +1;
|
||||
}
|
||||
}
|
||||
if (!current_pair) {
|
||||
pairs++;
|
||||
current_pair = pairs;
|
||||
init_pair(current_pair, terrain->foreground,
|
||||
terrain->background);
|
||||
color_pairs = realloc (color_pairs,
|
||||
sizeof *color_pairs * pairs);
|
||||
color_pairs[pairs-1] = malloc (sizeof **color_pairs);
|
||||
color_pairs[pairs-1][0] = terrain->foreground;
|
||||
color_pairs[pairs-1][1] = terrain->background;
|
||||
}
|
||||
attron (COLOR_PAIR (current_pair));
|
||||
mvprintw (printing_y, printing_x, "%c", symbol);
|
||||
attroff (COLOR_PAIR (1));
|
||||
attroff (COLOR_PAIR (current_pair));
|
||||
}
|
||||
}
|
||||
free (color_pairs);
|
||||
color_pairs = NULL;
|
||||
current_pos_map_x = game->x_pos - start_node_x;
|
||||
current_pos_map_y = game->y_pos - start_node_y;
|
||||
attron (A_BOLD);
|
||||
|
@ -200,8 +250,14 @@ l3tde_game_create_default_map (void) {
|
|||
nodes[i] = malloc (sizeof **nodes * nodes_len);
|
||||
L3TDEMapNodePtr *y_nodes = nodes[i];
|
||||
for (int j = 0; j < nodes_len; j++) {
|
||||
y_nodes[j] = l3tde_map_node_create (terrains[0],
|
||||
L3TDE_MAP_NODE_NAVIGATION_WALKABLE);
|
||||
if (rand() % 50 > 45) {
|
||||
y_nodes[j] = l3tde_map_node_create (
|
||||
terrains[1], 0);
|
||||
} else {
|
||||
y_nodes[j] = l3tde_map_node_create (
|
||||
terrains[0],
|
||||
L3TDE_MAP_NODE_NAVIGATION_WALKABLE);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -1,6 +1,12 @@
|
|||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <time.h>
|
||||
|
||||
#include <l3tde/game.h>
|
||||
|
||||
int
|
||||
main () {
|
||||
time_t t;
|
||||
srand((unsigned) time(&t));
|
||||
l3tde_game_init ();
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue