Horizon Official Technical Documentation
Coordinates.hpp
Go to the documentation of this file.
1/***************************************************
2 * _ _ _ *
3 * | | | | (_) *
4 * | |_| | ___ _ __ _ _______ _ __ *
5 * | _ |/ _ \| '__| |_ / _ \| '_ \ *
6 * | | | | (_) | | | |/ / (_) | | | | *
7 * \_| |_/\___/|_| |_/___\___/|_| |_| *
8 ***************************************************
9 * This file is part of Horizon (c).
10 *
11 * Copyright (c) 2019 Sagun K. (sagunxp@gmail.com).
12 * Copyright (c) 2019 Horizon Dev Team.
13 *
14 * Base Author - Sagun K. (sagunxp@gmail.com)
15 *
16 * This library is free software; you can redistribute it and/or modify
17 * it under the terms of the GNU General Public License as published by
18 * the Free Software Foundation, either version 3 of the License, or
19 * (at your option) any later version.
20 *
21 * This library is distributed in the hope that it will be useful,
22 * but WITHOUT ANY WARRANTY; without even the implied warranty of
23 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
24 * GNU General Public License for more details.
25 *
26 * You should have received a copy of the GNU General Public License
27 * along with this library. If not, see <http://www.gnu.org/licenses/>.
28 **************************************************/
29
30#ifndef HORIZON_ZONE_GAME_MAP_COORDINATES_HPP
31#define HORIZON_ZONE_GAME_MAP_COORDINATES_HPP
32
33#include <cinttypes>
34#include <cassert>
35#include <cmath>
36
37template <int16_t MAX_COORDINATES>
39{
40public:
41 Coordinates(int16_t x = 0, int16_t y = 0)
42 : _x(x), _y(y), _move_cost(0)
43 {
44 //
45 }
46
48 : _x(obj._x), _y(obj._y), _move_cost(obj._move_cost)
49 {
50 //
51 }
52
54 {
55 _x = right._x;
56 _y = right._y;
57 _move_cost = right._move_cost;
58 return *this;
59 }
60
62 {
63 return (_x == right._x && _y == right._y);
64 }
65
67 {
68 return !(*this == right);
69 }
70
72 {
73 return Coordinates<MAX_COORDINATES>(_x + right._x, _y + right._y);
74 }
75
77 {
78 return Coordinates<MAX_COORDINATES>(_x - right._x, _y - right._y);
79 }
80
81 template <int16_t BOUNDS>
82 bool is_within_range(Coordinates<BOUNDS> const &bounds, int range) const
83 {
84 int x_diff = _x - bounds.x();
85 int y_diff = _y - bounds.y();
86
87 return abs(x_diff) <= range && abs(y_diff) <= range;
88 }
89
90 template<int16_t BOUNDS>
92 {
93 int x = std::max(0, std::min((_x + range), (int) BOUNDS));
94 int y = std::max(0, std::min((_y + range), (int) BOUNDS));
95
96 return Coordinates<BOUNDS>(x, y);
97 }
98
99 // d=√((x2 – x1)² + (y2 – y1)²)
100 template<int16_t BOUNDS>
101 int distance_from(Coordinates<BOUNDS> const &bounds) const
102 {
103 int distance = std::sqrt(std::pow(bounds.x() - _x, 2) + std::pow(bounds.y() - _y, 2));
104 return distance;
105 }
106
107 template<int16_t BLOCK_SIZE, int16_t BLOCK_COUNT>
109 {
110 int x = _x / BLOCK_SIZE;
111 int y = _y / BLOCK_SIZE;
112
113 assert(x < BLOCK_COUNT && y < BLOCK_COUNT);
114 assert(x >= 0 && y >= 0);
115
117 }
118
119 int16_t x() const { return _x; }
120 int16_t y() const { return _y; }
121 int16_t move_cost() const { return _move_cost; }
123
124 void inc_x(int16_t val)
125 {
126 if (_x + val < MAX_COORDINATES)
127 _x += val;
128 else
129 _x = MAX_COORDINATES - 1;
130 }
131
132 void dec_x(int16_t val)
133 {
134 if (_x > val)
135 _x -= val;
136 else
137 _x = 0;
138 }
139
140 void inc_y(int16_t val)
141 {
142 if (_y + val < MAX_COORDINATES)
143 _y += val;
144 else
145 _y = MAX_COORDINATES - 1;
146 }
147
148 void dec_y(int16_t val)
149 {
150 if (_y > val)
151 _y -= val;
152 else
153 _y = 0;
154 }
155
156 bool valid() const
157 {
158 return _x < MAX_COORDINATES && _y < MAX_COORDINATES;
159 }
160
161private:
162 int16_t _x{0};
163 int16_t _y{0};
164 int16_t _move_cost{0};
165};
166
167#endif /* HORIZON_ZONE_GAME_MAP_COORDINATES_HPP */
Definition: Coordinates.hpp:39
Coordinates< MAX_COORDINATES > operator-(Coordinates< MAX_COORDINATES > const &right) const
Definition: Coordinates.hpp:76
int16_t _x
Definition: Coordinates.hpp:162
int16_t y() const
Definition: Coordinates.hpp:120
bool operator==(Coordinates< MAX_COORDINATES > const &right) const
Definition: Coordinates.hpp:61
Coordinates< BLOCK_COUNT > scale() const
Definition: Coordinates.hpp:108
void dec_x(int16_t val)
Definition: Coordinates.hpp:132
int16_t _move_cost
Definition: Coordinates.hpp:164
bool is_within_range(Coordinates< BOUNDS > const &bounds, int range) const
Definition: Coordinates.hpp:82
Coordinates(int16_t x=0, int16_t y=0)
Definition: Coordinates.hpp:41
Coordinates(const Coordinates< MAX_COORDINATES > &obj)
Definition: Coordinates.hpp:47
void dec_y(int16_t val)
Definition: Coordinates.hpp:148
int16_t _y
Definition: Coordinates.hpp:163
bool operator!=(Coordinates< MAX_COORDINATES > const &right) const
Definition: Coordinates.hpp:66
Coordinates< BOUNDS > at_range(int range) const
Definition: Coordinates.hpp:91
bool valid() const
Definition: Coordinates.hpp:156
Coordinates< MAX_COORDINATES > operator+(Coordinates< MAX_COORDINATES > const &right) const
Definition: Coordinates.hpp:71
void inc_y(int16_t val)
Definition: Coordinates.hpp:140
int distance_from(Coordinates< BOUNDS > const &bounds) const
Definition: Coordinates.hpp:101
void inc_x(int16_t val)
Definition: Coordinates.hpp:124
Coordinates< MAX_COORDINATES > operator=(const Coordinates< MAX_COORDINATES > &right)
Definition: Coordinates.hpp:53
int16_t move_cost() const
Definition: Coordinates.hpp:121
int16_t x() const
Definition: Coordinates.hpp:119
void set_move_cost(int16_t move_cost)
Definition: Coordinates.hpp:122