DungeonCrawl
Loading...
Searching...
No Matches
map_mode.c
Go to the documentation of this file.
1
5#include "map_mode.h"
6
7#include "../game.h"
10#include "../io/io_handler.h"
13#include "draw/draw_light.h"
14#include "map.h"
15
16#include <stdbool.h>
17#include <stdint.h>
18#include <string.h>
19
20vector2d_t map_anchor = {5, 2};
21vector2d_t player_pos;
22int player_has_key = 0;
23bool first_function_call = true;
24
25
26void set_player_start_pos(const int player_x, const int player_y) {
27 player_pos.dx = player_x;
28 player_pos.dy = player_y;
29 // at the start, tile under the player must be revealed
30 revealed_map[player_pos.dx][player_pos.dy] = FLOOR;
31}
32
34 return player_pos;
35}
36
37map_mode_result_t handle_input(const input_event_t* input_event, character_t* player) {
38 int new_x = player_pos.dx;
39 int new_y = player_pos.dy;
40
41 if (input_event->type == INPUT_QUIT) return QUIT;
42 if (input_event->type == INPUT_MENU) return SHOW_MENU;
43
44 // Process different modes
45 if (input_event->type == INPUT_INVENTORY) return SHOW_INVENTORY;
46 if (input_event->type == INPUT_STATS) return SHOW_STATS;
47
48 // Process movement using logical input types
49 switch (input_event->type) {
50 case INPUT_UP:
51 new_y--;
52 break;
53 case INPUT_DOWN:
54 new_y++;
55 break;
56 case INPUT_LEFT:
57 new_x--;
58 break;
59 case INPUT_RIGHT:
60 new_x++;
61 break;
62 default:
63 break;
64 }
65
66
67 if (new_x >= 0 && new_x < WIDTH && new_y >= 0 && new_y < HEIGHT) {
68 switch (map[new_x][new_y]) {
69 case WALL:
70 // ignore wall
71 // break;
72 case START_DOOR:
73 // ignore start door
74 break;
75 case KEY:
76 player_has_key = 1;
77 player_pos.dx = new_x;
78 player_pos.dy = new_y;
79 revealed_map[new_x][new_y] = FLOOR;
80 break;
81 case EXIT_DOOR:
82 if (player_has_key) {
83 player_has_key = 0;
84 player_pos.dx = new_x;
85 player_pos.dy = new_y;
86 return NEXT_FLOOR;
87 }
88 break;
89 case LIFE_FOUNTAIN:
90 player->current_resources.health = player->max_resources.health;
91 player_pos.dx = new_x;
92 player_pos.dy = new_y;
93 revealed_map[new_x][new_y] = FLOOR;
94 break;
95 case MANA_FOUNTAIN:
96 player->current_resources.mana = player->max_resources.mana;
97 player_pos.dx = new_x;
98 player_pos.dy = new_y;
99 revealed_map[new_x][new_y] = FLOOR;
100 break;
101 case GOBLIN:
102 player_pos.dx = new_x;
103 player_pos.dy = new_y;
104 map[new_x][new_y] = FLOOR;
105 revealed_map[new_x][new_y] = FLOOR;
106 return COMBAT;
107 default:
108 player_pos.dx = new_x;
109 player_pos.dy = new_y;
110 break;
111 }
112 }
113 return CONTINUE;
114}
115
116map_mode_result_t map_mode_update(character_t* player) {
117 map_mode_result_t next_state = CONTINUE;
118
119 if (!first_function_call) {
120 input_event_t input_event;
121
122 // Use our input handler to get input
123 if (get_input_nonblocking(&input_event)) {
124 next_state = handle_input(&input_event, player);
125 }
126 }
127 first_function_call = false;
128
129 // clear screen using the IO handler
130 clear_screen();
131 draw_light_on_player((map_tile_t*) map, (map_tile_t*) revealed_map, HEIGHT, WIDTH, player_pos, LIGHT_RADIUS);
132 draw_map_mode((const map_tile_t*) revealed_map, HEIGHT, WIDTH, map_anchor, player_pos);
133 // Use the centralized render function instead of direct notcurses call
134 render_frame();
135
136 return next_state;
137}
138
139void init_map_mode(void) {
140 for (int y = 0; y < HEIGHT; y++) {
141 for (int x = 0; x < WIDTH; x++) {
142 revealed_map[x][y] = HIDDEN;
143 }
144 }
145}
146
148 // Free any resources if needed
149}
void draw_light_on_player(map_tile_t *arr1, map_tile_t *arr2, int height, int width, vector2d_t player, const int light_radius)
Draws light around the player.
Definition draw_light.c:156
Exposes functions for drawing light on player.
Declares core game states, global database connection, and main game control functions.
bool get_input_nonblocking(input_event_t *event)
Get the next input event (non-blocking)
Exposes functions for working with input.
Exposes functions for working with the inventory mode.
Exposes functions for the IO-Handler.
Exposes common types and constants for the games map.
map_mode_result_t handle_input(const input_event_t *input_event, character_t *player)
Functions is only exposed for testing purposes.
Definition map_mode.c:37
map_mode_result_t map_mode_update(character_t *player)
Updates the player position based on the player's input and redraws the maze.
Definition map_mode.c:116
void init_map_mode(void)
Initializes the map mode.
Definition map_mode.c:139
vector2d_t get_player_pos()
Get the current player position.
Definition map_mode.c:33
void shutdown_map_mode(void)
Frees any resources associated with the map mode.
Definition map_mode.c:147
void set_player_start_pos(const int player_x, const int player_y)
Sets the starting position of the player.
Definition map_mode.c:26
Defines and manages functions for map exploration, player movement, and map interactions in map mode.
void draw_map_mode(const map_tile_t *arr, const int height, const int width, const vector2d_t anchor, const vector2d_t player_pos)
Draws the map mode UI based on the given parameters.
Definition map_output.c:14
Exposes functions for outputing to the screen while in the map mode.
bool render_frame(void)
Render the current frame.
void clear_screen(void)
Clear the screen.
Exposes functions for outputting to the console.
Structure for advanced input events with context data.
Definition input_types.h:43
2-dimensional vector struct
Definition common.h:164