feat: use vector to store player position data
This commit is contained in:
parent
d5b9ca8a7e
commit
d73dac0e67
1 changed files with 19 additions and 20 deletions
39
main.cpp
39
main.cpp
|
@ -19,19 +19,18 @@ const vector<vector<char>> play_field = { // playspace[y][x]
|
|||
*/
|
||||
|
||||
/// Check if the player could be moved to a certain space
|
||||
bool can_move_player(int pos_x, int pos_y, int move_x, int move_y);
|
||||
bool can_move_player(vector<int> player_pos, int move_x, int move_y);
|
||||
|
||||
/// Check if a certain space can be moved to
|
||||
bool is_pos_free(int x, int y);
|
||||
|
||||
/// Render the current playfield to the console
|
||||
void render_play_field(int pos_x, int pos_y);
|
||||
void render_play_field(vector<int> player_pos);
|
||||
|
||||
int main()
|
||||
{
|
||||
// Player (y, x)
|
||||
int pos_x = 0;
|
||||
int pos_y = 4;
|
||||
// Player position formatted as (y, x)
|
||||
vector<int> player_pos = {4, 0};
|
||||
|
||||
// Create input variable
|
||||
char input;
|
||||
|
@ -40,7 +39,7 @@ int main()
|
|||
while (true)
|
||||
{
|
||||
// Render the playfield
|
||||
render_play_field(pos_x, pos_y);
|
||||
render_play_field(player_pos);
|
||||
|
||||
// Get input
|
||||
cin >> input;
|
||||
|
@ -55,31 +54,31 @@ int main()
|
|||
{
|
||||
case 'w':
|
||||
// Check if it is possible to move player to target pos
|
||||
if (!can_move_player(pos_x, pos_y, 0, -1))
|
||||
if (!can_move_player(player_pos, 0, -1))
|
||||
continue;
|
||||
// Actually move player
|
||||
--pos_y;
|
||||
--player_pos[0];
|
||||
break;
|
||||
case 'a':
|
||||
// Check if it is possible to move player to target pos
|
||||
if (!can_move_player(pos_x, pos_y, -1, 0))
|
||||
if (!can_move_player(player_pos, -1, 0))
|
||||
continue;
|
||||
// Actually move player
|
||||
--pos_x;
|
||||
--player_pos[1];
|
||||
break;
|
||||
case 's':
|
||||
// Check if it is possible to move player to target pos
|
||||
if (!can_move_player(pos_x, pos_y, 0, 1))
|
||||
if (!can_move_player(player_pos, 0, 1))
|
||||
continue;
|
||||
// Actually move player
|
||||
++pos_y;
|
||||
++player_pos[0];
|
||||
break;
|
||||
case 'd':
|
||||
// Check if it is possible to move player to target pos
|
||||
if (!can_move_player(pos_x, pos_y, 1, 0))
|
||||
if (!can_move_player(player_pos, 1, 0))
|
||||
continue;
|
||||
// Actually move player
|
||||
++pos_x;
|
||||
++player_pos[1];
|
||||
break;
|
||||
case 'h':
|
||||
// Print help text to console
|
||||
|
@ -92,7 +91,7 @@ int main()
|
|||
}
|
||||
|
||||
// Check if the goal has been reached yet
|
||||
if (play_field[pos_y][pos_x] == 'Z')
|
||||
if (play_field[player_pos[0]][player_pos[1]] == 'Z')
|
||||
{
|
||||
// Goal reached! Lets celebrate!
|
||||
cout << "Ziel erreicht! Herzlichen Glueckwunsch!\n";
|
||||
|
@ -103,11 +102,11 @@ int main()
|
|||
return 0;
|
||||
}
|
||||
|
||||
bool can_move_player(const int pos_x, const int pos_y, const int move_x, const int move_y)
|
||||
bool can_move_player(const vector<int> player_pos, const int move_x, const int move_y)
|
||||
{
|
||||
// Calculate the position we want to be at
|
||||
const int target_x = pos_x + move_x;
|
||||
const int target_y = pos_y + move_y;
|
||||
const int target_x = player_pos[1] + move_x;
|
||||
const int target_y = player_pos[0] + move_y;
|
||||
|
||||
// Check if the position can be moved to
|
||||
const bool result = is_pos_free(target_x, target_y);
|
||||
|
@ -125,13 +124,13 @@ bool is_pos_free(const int x, const int y)
|
|||
return true; // Target pos is movable to (is not wall and not out of bounds)
|
||||
}
|
||||
|
||||
void render_play_field(const int pos_x, const int pos_y)
|
||||
void render_play_field(const vector<int> player_pos)
|
||||
{
|
||||
for (int i = 0; i < play_field.size(); ++i)
|
||||
{ // For every row...
|
||||
for (int j = 0; j < play_field[i].size(); ++j)
|
||||
{ // ... render ...
|
||||
if (i == pos_y && j == pos_x)
|
||||
if (i == player_pos[0] && j == player_pos[1])
|
||||
cout << "S "; // ... 'S' if it is the entry where the player is currently
|
||||
else
|
||||
cout << play_field[i][j]; // ... the actual entry of the field
|
||||
|
|
Loading…
Reference in a new issue