-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGoo.cpp
More file actions
311 lines (293 loc) · 10.3 KB
/
Goo.cpp
File metadata and controls
311 lines (293 loc) · 10.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
#include "Goo.hpp"
#include <iostream>
#include <functional>
#include <algorithm>
#include <limits>
#include <random>
#include <chrono>
namespace StrategyGoo
{
size_t RandomRange( int min, int max )
{
std::uniform_int_distribution< int > distribution{ min, max };
std::mt19937 randomDevice(
abs( static_cast< int >( std::chrono::system_clock::now().time_since_epoch().count() ) ) );
return distribution( randomDevice );
}
Goo::Goo( entt::registry& registry_, BoardPosition start, GameBoard* board_, size_t tileWidth, size_t tileHeight ) :
Updator( registry_, start, board_, tileWidth, tileHeight, "CubeGoo1" ) {
InitilizeRefrences< GooRefrence, Goo >( *this );
//TODO after debugging.//
RefrenceSprite().SetActive( false );
AddGoo( start );
}
Goo::GooComponent::GooComponent( entt::registry& registry_, BoardPosition start, GameBoard* board_,
size_t tileWidth, size_t tileHeight, Goo* parent_ ) :
Updator( registry_, start, board_, tileWidth, tileHeight, "CubeGoo1" ), parent( parent_ ) {
InitilizeRefrences< Goo::GooComponentRefrence, Goo::GooComponent >( *this );
MakeGooAnimation();
RefrenceSprite().RefrenceSprite().scale( .63f, .5f );
RefrenceSprite().RefrenceSprite().setOrigin( 0.f, 16.f );
RefrenceSprite().SetAnimationRate( 1.f / 5.f );
}
void Goo::GooComponent::MakeGooAnimation()
{
ANIMATION_TYPE animation;
for( size_t i = 0; i < AMOUNT_OF_DIRECTIONS_CONSTANT; ++i )
{
const auto AMOUNT_OF_FRAMES_FOR_DIRECTION_CONSTANT = RefrenceSprite().ObtainFramesForDirection( ( Direction ) i ).size();
for( size_t j = 0; j < AMOUNT_OF_FRAMES_FOR_DIRECTION_CONSTANT; ++j )
RefrenceSprite().RefrenceAnimations()[ 0 ][ 0 ].push_back( ( size_t ) RandomRange( 0, ( int ) AMOUNT_OF_FRAMES_FOR_DIRECTION_CONSTANT - 1 ) );
}
}
Goo::GooComponent& Goo::AddGoo( BoardPosition location )
{
GooComponent* newGoo = new GooComponent(
registry, location, board, ( size_t ) TILE_WIDTH_CONSTANT, ( size_t ) TILE_HEIGHT_CONSTANT, this );
goo.push_back( newGoo );
return *newGoo;
}
Goo::EmptySquareLocalType Goo::FindEmptyAround( GooComponent& toLookAround, size_t spreadReach )
{
auto position = toLookAround.RefrenceBoardPosition();
GameBoard& board = *toLookAround.GetBoard();
EmptySquareLocalType emptySquares;
auto checkForGoo = [ & ]( Tile& tile ) {
return toLookAround.RefrenceRegistry().has<
GooComponentRefrence >( tile.GetID() );
};
for( size_t y = ( ( ( size_t ) position.y >= spreadReach ) ? position.y - spreadReach : 0 );
y <= ( position.y + spreadReach ) && y < board.GetHeight(); ++y )
{
for( size_t x = ( ( ( size_t ) position.x >= spreadReach ) ? position.x - spreadReach : 0 );
x <= ( position.x + spreadReach ) && x < board.GetWidth(); ++x ) {
if( checkForGoo( board[ x ][ y ] ) == false )
emptySquares.push_back( BoardPosition( ( int ) x, ( int ) y ) );
}
}
return emptySquares;
}
Goo::EmptySquareType Goo::FindEmptySquares( Goo& goo, size_t spreadReach )
{
Goo::EmptySquareType allEmptySquares;
std::vector< Goo::GooComponent* >& allGoo = goo.GetGoo();
for( Goo::GooComponent* currentGoo : allGoo ) {
allEmptySquares.push_back( std::pair< Goo::GooComponent&, Goo::EmptySquareLocalType >(
*currentGoo, Goo::FindEmptyAround( *currentGoo, spreadReach ) ) );
}
return allEmptySquares;
}
bool Goo::RemoveGoo( GooRefrence& toRemove )
{
const size_t AMOUNT_OF_GOO_COMPONENTS_CONSTANT = goo.size();
for( size_t i = 0; i < AMOUNT_OF_GOO_COMPONENTS_CONSTANT; ++i ) {
if( goo[ i ]->GetID() == toRemove.get().GetID() ) {
return RemoveGoo( i );
}
}
return false;
}
bool Goo::RemoveGoo( GooComponent& toRemove )
{
const size_t AMOUNT_OF_GOO_COMPONENTS_CONSTANT = goo.size();
for( size_t i = 0; i < AMOUNT_OF_GOO_COMPONENTS_CONSTANT; ++i ) {
if( goo[ i ]->GetID() == toRemove.GetID() ) {
return RemoveGoo( i );
}
}
return false;
}
bool Goo::RemoveGoo( GooComponent* toRemove )
{
const size_t AMOUNT_OF_GOO_COMPONENTS_CONSTANT = goo.size();
for( size_t i = 0; i < AMOUNT_OF_GOO_COMPONENTS_CONSTANT; ++i ) {
if( goo[ i ] == toRemove ) {
return RemoveGoo( i );
}
}
return false;
}
bool Goo::RemoveGoo( BoardPosition toRemove )
{
const size_t AMOUNT_OF_GOO_COMPONENTS_CONSTANT = goo.size();
for( size_t i = 0; i < AMOUNT_OF_GOO_COMPONENTS_CONSTANT; ++i ) {
if( goo[ i ]->RefrenceBoardPosition() == toRemove ) {
return RemoveGoo( i );
}
}
return false;
}
bool Goo::RemoveGoo( size_t toRemove )
{
bool status = false;
if( toRemove < goo.size() )
{
RemoveEntityFromTile< GooComponentRefrence >(
registry, goo[ toRemove ]->RefrenceBoardPosition(), board );
registry.remove_all( goo[ toRemove ]->GetID() );
registry.destroy( goo[ toRemove ]->GetID() );
delete goo[ toRemove ];
goo.erase( goo.begin() + toRemove );
status = true;
}
return status;
}
// * TODO: Add wall avoidence later? Could be done throug exclude or by not allowing it in the rules of FindEmptySquares//
bool Goo::MoveToward( BoardPosition where, size_t spreadReach, bool grow, std::vector< GooComponent* > exclude )
{
if( spreadReach > 0 )
{
GooComponent* closest = ClosestToPoint( where, false, exclude );
GooComponent* furthest = ClosestToPoint( where, true, exclude );
if( where == closest->RefrenceBoardPosition() )
return true;
BoardPosition unit = ToUnitVector< int >( where - closest->RefrenceBoardPosition() );
if( goo.size() == 1 ) {
//std::cout << "Closest == Furthest!\n";
return MoveEntity< GooComponentRefrence >( registry, GooComponentRefrence( *closest ),
closest->RefrenceBoardPosition() += unit, board );
}
else
{
//std::cout << "Moving!\n";
auto empty = FindEmptyAround( *closest );
const size_t AMOUNT_OF_EMPTY_TILES = empty.size();
if( AMOUNT_OF_EMPTY_TILES <= 0 )
{
//std::cout << "NO EMPTY TILES!!\n";
bool nowhereToMove = true;
for( auto* excluded : exclude ) {
for( auto* currentGoo : goo )
nowhereToMove = ( nowhereToMove && ( currentGoo == excluded ) );
}
if( nowhereToMove == true )
return false;
exclude.push_back( closest );
return MoveToward( where, spreadReach, grow, exclude );
}
BoardPosition newLocation = empty[ ( size_t ) RandomRange( 0, ( int ) AMOUNT_OF_EMPTY_TILES - 1 ) ];
if( grow == true )
AddGoo( newLocation );
else if( MoveEntity< GooComponentRefrence >( registry, *furthest, newLocation, board ) == true )
{
//std::cout << "Move made\n";
furthest->RefrenceBoardPosition() = newLocation;
furthest->RefrenceSprite().RefrenceSprite().setPosition( furthest->ToWorldPosition() );
}
else {
//std::cout << "MOVE FAILURE\n";
return false;
}
//std::cout << "Done A\n";
return MoveToward( where, spreadReach - 1, grow, exclude );
}
}
//std::cout << "Done B!\n";
return true;
}
Goo::GooComponent* Goo::ClosestToPoint( BoardPosition point, bool invert, std::vector< GooComponent* > exclude )
{
GooComponent* currentClosest = nullptr;
if( goo.size() > 0 )
{
std::function< bool( size_t, size_t ) > comparison;
if( invert == false )
comparison = std::less< size_t >();
else
comparison = ( std::greater< size_t >() );
size_t closestDistance = !invert ? std::numeric_limits< size_t >().max() :
std::numeric_limits< size_t >().min();
size_t currentMagnitude = closestDistance;
for( auto* currentGoo : goo )
{
bool excluded = false;
for( auto* excludedGoo : exclude )
excluded = ( excludedGoo == currentGoo );
currentMagnitude = ( size_t ) Magnitude( point - currentGoo->RefrenceBoardPosition() );
if( comparison( currentMagnitude, closestDistance ) == true ) {
currentClosest = currentGoo;
closestDistance = currentMagnitude;
}
}
}
return currentClosest;
}
std::vector< Goo::GooComponent* >& Goo::GetGoo() {
return goo;
}
bool ShootGrenadeOrder::Tick( Squaddie& squaddie, entt::registry& registry )
{
bool status = false;
auto getSpriteComponent = [&]( entt::entity id ) -> Sprite< 0 >& {
return registry.get< Sprite< 0 > >( id );
};
auto getBoardPositionComponent = [&]( entt::entity id ) -> BoardPosition& {
return registry.get< BoardPosition >( id );
};
auto updateSprite = [&]( entt::entity id, BoardPosition whereTo ) {
getSpriteComponent( id ).RefrenceSprite().setPosition(
squaddie.GetBoard()->ToWorldCoordinates( whereTo ) );
};
auto moveSprite = [&]( entt::entity id, sf::Vector2f displacement ) {
getSpriteComponent( id ).RefrenceSprite().move( ( float ) displacement.x, ( float ) displacement.y );
getBoardPositionComponent( id ) = squaddie.GetBoard()->ToBoardCoordinates(
getSpriteComponent( id ).RefrenceSprite().getPosition() );
};
if( createdGrenade == false )
{
//std::cout << "A\n";
grenadeID = registry.create();
registry.emplace< Sprite< 0 > >( grenadeID, "Grenade" );
registry.emplace< BoardPosition >( grenadeID, from );
updateSprite( grenadeID, from );
createdGrenade = true;
}
else if( createdGrenade == true && detonatedGrenade == false )
{
//std::cout << "B\n";
auto boardPosition = getBoardPositionComponent( grenadeID );
//PrintVect( boardPosition );
BoardPosition displacement = to - boardPosition;
if( displacement == BoardPosition( 0, 0 ) )
{
registry.remove_all( grenadeID );
registry.destroy( grenadeID );
explosionID = registry.create();
registry.emplace< Sprite< 0 > >( explosionID, "CubeExplosion1" );
registry.emplace< BoardPosition >( explosionID, to );
updateSprite( explosionID, to );
detonatedGrenade = true;
}
else {
squaddie.RefrenceSprite().SetCurrentDirection( ALL_DIRECTIONS_CONSTANT[ ClosestFacing( -displacement ) ] );
moveSprite( grenadeID, ToUnitVector( ConvertVector< float, int >( displacement ) ).result * 5.f );
}
}
else
{
if( killedGoo == false )
{
Goo* gooParent = nullptr;
registry.view< Goo::GooComponentRefrence, Tile::TileRefrence >().each(
[&]( Goo::GooComponentRefrence& goo, Tile::TileRefrence& tile )
{
if( goo.get().RefrenceBoardPosition() == to ) {
gooParent = goo.get().parent;
}
}
);
if( gooParent != nullptr )
gooParent->RemoveGoo( to );
killedGoo = true;
}
if( explosionTime++ >= maxExplosionTime )
{
//std::cout << "C\n";
registry.remove_all( explosionID );
registry.destroy( explosionID );
status = true;
}
}
return status;
}
}