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
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437 |
#include "fsCView.h"
#include <fsRenderer/src/components/fsCSpriteComponent.h>
#include <fsRenderer/src/fsCBrushManager.h>
#include <fsAppCore/src/fsCEntityFactory.h>
#include <fsCore/fsSColour.h>
#include <fsCore/src/fsCResourceName.h>
#include "../fsIScroller.h"
#include "../fsITileMap.h"
#include "../fsITile.h"
#include "../fsIObject.h"
#include "fsCore/src/fsNMaths.h"
#include "fsRenderer/fsITexture.h"
using namespace fsNTileMap;
fsPoint& fsCView::scrollOffsetGet()
{
return mScrollOffset;
}
fsS32 fsCView::pixelWidtheGet() const
{
return mViewTileWidth * mMap->tileWidthGet() * mScale;
}
fsS32 fsCView::pixelHeightGet() const
{
return mViewTileHeight * mMap->tileHeightGet() * mScale;
}
fsPoint fsCView::screenToMap(fsPoint pPos) const
{
pPos.x /= mScale;
pPos.y /= mScale;
fsPoint relativePos = {pPos.x + mOrigin.x + mScroller->tileOffsetGet().x * mMap->tileWidthGet(),
pPos.y + mOrigin.y + mScroller->tileOffsetGet().y * mMap->tileHeightGet()};
return relativePos;
}
fsPoint fsCView::mapToView(fsPoint pPos) const
{
fsPoint relativePos = {pPos.x - mOrigin.x - mScroller->tileOffsetGet().x * mMap->tileWidthGet(),
pPos.y - mOrigin.y - mScroller->tileOffsetGet().y * mMap->tileHeightGet()};
return relativePos;
}
fsPoint fsCView::mapPixelToLocal(const fsPoint& pMapPixel) const
{
// The map's scaled pixel box is centred on the view: -mOrigin is the view
// centre in local space, mapHalf steps back to the box's top-left corner, and
// the map pixel itself steps forward by the render scale.
const fsS32 mapHalfW = static_cast<fsS32>(mMap->widthGet() * mMap->tileWidthGet() * mScale / 2);
const fsS32 mapHalfH = static_cast<fsS32>(mMap->heightGet() * mMap->tileHeightGet() * mScale / 2);
return fsPoint(-mOrigin.x - mapHalfW + static_cast<fsS32>(pMapPixel.x * mScale),
-mOrigin.y - mapHalfH + static_cast<fsS32>(pMapPixel.y * mScale));
}
fsBool fsCView::check(fsPoint const& pPos)
{
static fsS32 in = -1;
if (in!=-1)
{
if (in > 0 && in < mLayers[0].mTiles.size())
{
auto sprt = mLayers[0].mTiles[in]->spriteComponentGet();
sprt->colourSet(fsSColour4B::fsWHITE());
}
}
auto mapPos = mapToView(pPos);
in = mapPos.x / mMap->tileWidthGet() + mapPos.y / mMap->tileHeightGet() * mViewTileWidth;
if (in < 0 || in >= mLayers[0].mTiles.size())
{
return false;
}
auto sprt = mLayers[0].mTiles[in]->spriteComponentGet();<--- Shadowed variable
auto mapTileCoords = mapPos;
mapTileCoords.x /= mMap->tileWidthGet();
mapTileCoords.y /= mMap->tileHeightGet();
auto mapTile = tileGet(mLayers[0].mId, mapTileCoords.x, mapTileCoords.y);
if (mMap->collidable(mapTile->tileSetIdGet(), mapTile->idGet()))
{
sprt->colourSet(fsSColour4B(255, 0, 0, 190));
return false;
}
else
{
sprt->colourSet(fsSColour4B(0, 255, 0, 190));
return true;
}
return true;
fsS32 leftTile = pPos.x / mMap->tileWidthGet();
fsS32 rightTile = pPos.x / mMap->tileWidthGet();
fsS32 topTile = pPos.x / mMap->tileWidthGet();
fsS32 bottomTile = pPos.x / mMap->tileWidthGet();
fsPoint tileOffset = mScroller->tileOffsetGet();
if (leftTile < mViewTileWidth
&& topTile < mViewTileHeight)
{
for (fsS32 j = topTile; j <= bottomTile; ++j)
{
for (fsS32 i = leftTile; i <= rightTile; ++i)
{
for (STiledLayer& layer : mLayers)
{
auto sprt = layer.mTiles[i + j * mViewTileWidth]->spriteComponentGet();<--- Shadow local variable
sprt->colourSet(fsSColour4B(0, 255, 0, 190));
}
}
}
}
return true;
}
void fsCView::jumpTo(const fsPoint& pPos)
{
mScroller->tileOffsetSet(pPos);
for (STiledLayer& layer : mLayers)<--- Variable 'layer' can be declared as reference to const
{
fsS32 currRow = 0;
fsS32 currCol = 0;
for (fsCEntity* const tile : layer.mTiles)
{
tileBrushSet(tile, layer.mId, currCol, currRow);
if (++currCol >= mViewTileWidth)
{
currCol = 0;
if (++currRow + mScroller->subTileScrollOffsetGet().y >= mMap->heightGet())
{
--currRow;
}
}
}
}
}
void fsCView::scroll(fsS32& pAttemptedX, fsS32& pAttemptedY)
{
fsBool tileWrap = false;
auto step = pAttemptedX;
auto stepSize = fsNMaths::signCopy(mMap->tileWidthGet(), step);
while (stepSize
&& (fsNMaths::absS32(step) > fsNMaths::absS32(stepSize))
&& ((step >= 0 && stepSize >= 0) || (step < 0 && stepSize < 0)))
{
step -= stepSize;
tileWrap |= mScroller->fullTileScrollHandleX(stepSize, mViewTileWidth);
}
tileWrap |= mScroller->fullTileScrollHandleX(step, mViewTileWidth);
pAttemptedX = step;
// mScrollOffset.x = (mScroller->tileOffsetGet().x * mMap->tileWidthGet() );
stepSize = mMap->tileHeightGet();
step = pAttemptedY;
while (stepSize
&& (fsNMaths::absS32(step) > fsNMaths::absS32(stepSize))
&& ((step >= 0 && stepSize >= 0) || (step < 0 && stepSize < 0)))
{
step -= stepSize;
tileWrap |= mScroller->fullTileScrollHandleY(stepSize, mViewTileHeight);
}
tileWrap |= mScroller->fullTileScrollHandleY(step, mViewTileHeight);
pAttemptedY = step;
// mScrollOffset.y = (mScroller->tileOffsetGet().y * mMap->tileHeightGet() + pAttemptedY) * mScale;
scrollMap(0, step, tileWrap);
}
void fsCView::scrollMap(fsS32 pX, fsS32 pY, fsBool pTileWrap)
{
for (STiledLayer& layer: mLayers)
{
if (layer.mLayerType == STiledLayer::eTILE_MAP)
{
const fsPoint offset(layer.mRootEntity->posGet().x,
layer.mRootEntity->posGet().y);
fsS32 currRow = 0;
fsS32 currCol = 0;
for (fsCEntity* const tile: layer.mTiles)
{
fsPoint pos;
if (pTileWrap)
{
pos = (fsPoint(offset.x + currCol * mMap->tileWidthGet(),
offset.y + currRow * mMap->tileHeightGet()));
tileBrushSet(tile, layer.mId,
currCol,
currRow);
pos.x *= mScale;
pos.y *= mScale;
}
else
{
pos = tile->posGet();
pos.x += pX * mScale;
pos.y += pY * mScale;
}
tile->posSet(pos);
if (++currCol >= mViewTileWidth)
{
currCol = 0;
++currRow;
}
}
}
else if (layer.mLayerType == STiledLayer::eTRIGGERS)
{
std::for_each(std::begin(layer.mTriggers), std::end(layer.mTriggers),
[&](auto trigger)
{
if (trigger->mEntity)
{
trigger->mEntity->posSet(
{
trigger->mSource->posGet().x -
mScroller->tileOffsetGet().x * mMap->tileWidthGet() * mScale,
trigger->mSource->posGet().y -
mScroller->tileOffsetGet().y * mMap->tileHeightGet() * mScale,
});
}
});
}
}
}
fsITileRef fsCView::tileGet(fsS32 pLayer, fsS32 pCol, fsS32 pRow) const
{
return mMap->tileGet(pLayer, mScroller->tileOffsetGet().x + pCol, mScroller->tileOffsetGet().y + pRow);
}
fsBool fsCView::tileHide(fsS32 pLayerId, fsS32 pCol, fsS32 pRow)
{
for (STiledLayer& layer : mLayers)
{
if (layer.mLayerType != STiledLayer::eTILE_MAP || layer.mId != pLayerId)
{
continue;
}
// tileLayerCreate() only spawns an entity for non-empty cells, pushed in
// row-major order, so a cell's entity index is the count of non-empty
// cells before it. Walk the map the same way to find (pCol,pRow)'s entity.
fsS32 packedIndex = 0;
for (fsS32 row = 0; row < mViewTileHeight; ++row)
{
for (fsS32 col = 0; col < mViewTileWidth; ++col)
{
if (mMap->tileGet(pLayerId, col, row)->tileSetIdGet() == -1)
{
continue;
}
if (col == pCol && row == pRow)
{
if (packedIndex < static_cast<fsS32>(layer.mTiles.size()))
{
layer.mTiles[packedIndex]->visibleSet(false);
return true;
}
return false;
}
++packedIndex;
}
}
return false;
}
return false;
}
void fsCView::tileBrushSet(fsCEntity* const pEnt, fsS32 pLayer, fsS32 pCol, fsS32 pRow)
{
auto tile = tileGet(pLayer, pCol, pRow);
if (-1 != tile->tileSetIdGet())
{
auto* const sprt = pEnt->spriteComponentGet();
//mMap->tileBrushGet(tile->tileSetIdGet(), tile->idGet())->textureGet()->wrapModeSet(fsITexture::fsWrapMode::eCLAMP_TO_EDGE);
mMap->tileBrushGet(tile->tileSetIdGet(), tile->idGet())->textureGet()->filterModeSet(fsITexture::fsFilterMode::eNEAREST);
sprt->brushSet(mMap->tileBrushGet(tile->tileSetIdGet(), tile->idGet()));
sprt->widthSet(sprt->widthGet() * mScale);
sprt->heightSet(sprt->heightGet() * mScale);
// if (mMap->collidable(tile->tileSetIdGet(), tile->idGet()))
// {
// sprt->colourSet(fsCColour(1.0f, 0.0f, 0.0f, 0.75f));
// }
// else
// {
// sprt->colourSet(fsCColour(1.0f, 1.0f, 1.0f, 1.0f));
// }
}
}
fsCEntity* const fsCView::tileEntityCreate(fsS32 pLayer, fsS32 pCol, fsS32 pRow)
{
auto entityFactory = fsCAppCoreMain::instanceGet()->sceneRootGet()->entityFactoryGet();
fsCEntity* ent = entityFactory->emptyEntityCreate("");
ent->componentAdd(fsCSpriteComponent::mTypeId);
ent->posSet(fsPoint(pCol * mMap->tileWidthGet() * mScale, pRow * mMap->tileHeightGet() * mScale));
tileBrushSet(ent, pLayer, pCol, pRow);
return ent;
}
void fsCView::triggerLayerCreate(fsS32 pLayer)
{
mLayers.push_back(STiledLayer(mMap->triggersGet(pLayer), pLayer));
}
void fsCView::tileLayerCreate(fsS32 pLayer)
{
auto entityFactory = fsCAppCoreMain::instanceGet()->sceneRootGet()->entityFactoryGet();
fsCEntity* const tileLayerRootEnt = entityFactory->emptyEntityCreate("layer" + fsStr(pLayer));
// tileLayerRootEnt->posSet(mOrigin);
std::vector< fsCEntity* > layer;
for (fsS32 currRow = 0; currRow < mViewTileHeight; ++currRow)
{
for (fsS32 currCol = 0; currCol < mViewTileWidth; ++currCol)
{
if (mMap->tileGet(pLayer, currCol, currRow)->tileSetIdGet() != -1)
{
layer.push_back(tileEntityCreate(pLayer, currCol, currRow));
tileLayerRootEnt->childAdd(layer.back());
}
}
}
childAdd(tileLayerRootEnt);
mLayers.push_back(STiledLayer(tileLayerRootEnt,layer, pLayer));
}
void fsCView::imageLayerCreate(const fsS32 pLayer)
{
auto entityFactory = fsCAppCoreMain::instanceGet()->sceneRootGet()->entityFactoryGet();
fsCEntity* const ent = entityFactory->emptyEntityCreate("layer" + fsStr(pLayer));
ent->componentAdd(fsCSpriteComponent::mTypeId);
auto* const sprt = ent->spriteComponentGet();
sprt->brushSet(
fsCBrushManager::instanceGet()->get(
fsCResourceName(mMap->imagePathGet(pLayer), fsCResourceName::eRES_LOCATION_ASSETS), false));
// brushSet auto-sizes the sprite to the image's native pixels; scale it to the
// view's render scale so the backdrop covers the same area the (scaled) tile
// grid does - mirrors tileBrushSet().
sprt->widthSet(sprt->widthGet() * mScale);
sprt->heightSet(sprt->heightGet() * mScale);
// The layer's authored Tiled offset is its top-left in map-pixel space;
// mapPixelToLocal centres the whole map box on the view, and since sprites are
// centre-anchored we add half the (scaled) sprite size to reach its centre. A
// full-map backdrop (offset 0, map-sized) lands dead centre; a smaller layer
// like hills sits wherever its offset places it.
const fsPoint topLeft = mapPixelToLocal(mMap->layerOffsetGet(pLayer));
ent->posSet(fsPoint(topLeft.x + sprt->widthGet() / 2,
topLeft.y + sprt->heightGet() / 2));
childAdd(ent);
}
fsCView::STiledLayer::STiledLayer(std::vector<fsIObjectRef> pSourceObjects, fsS32 pId):
mRootEntity(nullptr),
mId(pId),
mLayerType(eTRIGGERS)
{
std::for_each(std::begin(pSourceObjects), std::end(pSourceObjects),
[&](fsIObjectRef src)
{
mTriggers.push_back( new STrigger(src));
});
}
std::vector<fsCView::STrigger*>const & fsCView::triggersGet()
{
return mLayers[1].mTriggers;
}
fsCView* const fsCView::create(fsITileMap* const pMap, fsIScroller* const pScroller, fsS32 pViewWidth, fsS32 pViewHeight, fsF32 pScale)
{
fsCView* const view = new fsCView(pMap, pScroller, pViewWidth, pViewHeight, pScale);
pScroller->tileMapSet(pMap);
pScroller->tileOffsetSet(pMap->startLocationGet());
for (fsS32 layer = 0 ; layer < pMap->layerCountGet(); ++layer)
{
if (fsILayer::eType::TILE == pMap->layerTypeGet(layer))
{
view->tileLayerCreate(layer);
}
else if (fsILayer::eType::OBJECT == pMap->layerTypeGet(layer))
{
view->triggerLayerCreate(layer);
}
else if (fsILayer::eType::IMAGE == pMap->layerTypeGet(layer))
{
view->imageLayerCreate(layer);
}
}
view->posSet(view->mOrigin);
return view;
}
fsCView::fsCView(fsITileMap* const pMap, fsIScroller* const pScroller, fsS32 pViewWidth, fsS32 pViewHeight, fsF32 pScale) :
fsCEntity("tilemapView"),
mScale(pScale),
mPixelStride(pMap->tileWidthGet() * pViewWidth, pMap->tileHeightGet() * pViewHeight),
mOrigin(-pViewWidth / 2.0f * pMap->tileWidthGet() * mScale, -pViewHeight / 2.0f * pMap->tileHeightGet() * mScale),
mViewTileWidth(pViewWidth),
mViewTileHeight(pViewHeight),
mMap(pMap),
mScroller(pScroller)
{
}
fsCView::~fsCView()
{
delete mMap;
delete mScroller;
}
|