DungeonCrawl
Loading...
Searching...
No Matches
map_mode.c File Reference

Implements functions for the map mode of the game. More...

#include "map_mode.h"
#include "../game.h"
#include "../inventory/inventory_mode.h"
#include "../io/input/input_handler.h"
#include "../io/io_handler.h"
#include "../io/output/common/output_handler.h"
#include "../io/output/specific/map_output.h"
#include "draw/draw_light.h"
#include "map.h"
#include <stdbool.h>
#include <stdint.h>
#include <string.h>

Go to the source code of this file.

Functions

void set_player_start_pos (const int player_x, const int player_y)
 Sets the starting position of the player.
vector2d_t get_player_pos ()
 Get the current player position.
map_mode_result_t handle_input (const input_event_t *input_event, character_t *player)
 Functions is only exposed for testing purposes.
map_mode_result_t map_mode_update (character_t *player)
 Updates the player position based on the player's input and redraws the maze.
void init_map_mode (void)
 Initializes the map mode.
void shutdown_map_mode (void)
 Frees any resources associated with the map mode.

Variables

vector2d_t map_anchor = {5, 2}
vector2d_t player_pos
int player_has_key = 0
bool first_function_call = true

Detailed Description

Implements functions for the map mode of the game.

Definition in file map_mode.c.

Function Documentation

◆ get_player_pos()

vector2d_t get_player_pos ( )

Get the current player position.

Returns
the player position as a vector2d_t structure

Definition at line 33 of file map_mode.c.

33 {
34 return player_pos;
35}

◆ handle_input()

map_mode_result_t handle_input ( const input_event_t * input_event,
character_t * player )

Functions is only exposed for testing purposes.

Definition at line 37 of file map_mode.c.

37 {
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}

◆ init_map_mode()

void init_map_mode ( void )

Initializes the map mode.

Definition at line 139 of file map_mode.c.

139 {
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}

◆ map_mode_update()

map_mode_result_t map_mode_update ( character_t * player)

Updates the player position based on the player's input and redraws the maze.

Returns
CONTINUE (0) if the game continue, QUIT (1) if the player pressed the exit key.

Definition at line 116 of file map_mode.c.

116 {
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}
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
bool get_input_nonblocking(input_event_t *event)
Get the next input event (non-blocking)
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
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
bool render_frame(void)
Render the current frame.
void clear_screen(void)
Clear the screen.
Structure for advanced input events with context data.
Definition input_types.h:43

◆ set_player_start_pos()

void set_player_start_pos ( int player_x,
int player_y )

Sets the starting position of the player.

Parameters
player_xThe starting x position of the player.
player_yThe starting y position of the player.

Definition at line 26 of file map_mode.c.

26 {
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}

◆ shutdown_map_mode()

void shutdown_map_mode ( void )

Frees any resources associated with the map mode.

Definition at line 147 of file map_mode.c.

147 {
148 // Free any resources if needed
149}

Variable Documentation

◆ first_function_call

bool first_function_call = true

Definition at line 23 of file map_mode.c.

◆ map_anchor

vector2d_t map_anchor = {5, 2}

Definition at line 20 of file map_mode.c.

20{5, 2};

◆ player_has_key

int player_has_key = 0

Definition at line 22 of file map_mode.c.

◆ player_pos

vector2d_t player_pos

Definition at line 21 of file map_mode.c.