Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
TileViewer.cpp
00001 #include "mbed.h" 00002 #include "TileViewer.h" 00003 #include "GameEngine.h" 00004 00005 #define max(a, b) ((a < b) ? b : a) 00006 #define min(a, b) ((a > b) ? b : a) 00007 00008 00009 TileViewer::TileViewer(uint8_t viewTilesX, uint8_t viewTilesY) : 00010 _bitmap(viewTilesX * 8, viewTilesY * 8), 00011 _canvas(&_bitmap), 00012 _viewTilesX(viewTilesX), 00013 _viewTilesY(viewTilesY), 00014 _map(NULL), 00015 _mapTilesX(0), 00016 _mapTilesY(0), 00017 _offsetX(0), 00018 _offsetY(0), 00019 _blocks(NULL), 00020 _sprites(NULL), 00021 _pTrackedGameObject(NULL) 00022 { 00023 } 00024 00025 void TileViewer::setMap(const uint8_t *map, uint8_t mapTilesX, uint8_t mapTilesY, const Block *blocks, Sprite *sprites) 00026 { 00027 _map = map; 00028 _mapTilesX = mapTilesX; 00029 _mapTilesY = mapTilesY; 00030 _blocks = blocks; 00031 _sprites = sprites; 00032 } 00033 00034 void TileViewer::addGameObject(GameObject *gameObject) 00035 { 00036 for (int i = 0; i < MAX_GAMEOBJECTS; ++i) 00037 { 00038 if (_gameObjects[i] == NULL) 00039 { 00040 _gameObjects[i] = gameObject; 00041 gameObject->setParent(this); 00042 break; 00043 } 00044 } 00045 } 00046 00047 void TileViewer::removeGameObject(GameObject *gameObject) 00048 { 00049 for (int i = 0; i < MAX_GAMEOBJECTS; ++i) 00050 { 00051 if (_gameObjects[i] == gameObject) 00052 { 00053 gameObject->setParent(NULL); 00054 _gameObjects[i] = NULL; 00055 break; 00056 } 00057 } 00058 } 00059 00060 void TileViewer::track(GameObject *pGameObject) 00061 { 00062 _pTrackedGameObject = pGameObject; 00063 } 00064 00065 const GameObject* TileViewer::detectCollision(GameObject *primary) 00066 { 00067 for (int i = 0; i < MAX_GAMEOBJECTS; ++i) 00068 { 00069 GameObject *other = _gameObjects[i]; 00070 if (other != NULL && other != primary) 00071 { 00072 if (detectCollision(primary, other)) 00073 { 00074 return other; 00075 } 00076 } 00077 } 00078 return NULL; 00079 } 00080 00081 bool TileViewer::detectCollision(GameObject *o1, GameObject *o2) 00082 { 00083 Rect r1 = o1->getCollisionRect(); 00084 Rect r2 = o2->getCollisionRect(); 00085 00086 return r1.left < r2.right && 00087 r2.left < r1.right && 00088 r1.top < r2.bottom && 00089 r2.top < r1.bottom; 00090 } 00091 00092 const Block* TileViewer::detectBlock(GameObject *primary) 00093 { 00094 return NULL; 00095 } 00096 00097 void TileViewer::centerAt(int16_t x, int16_t y) 00098 { 00099 _offsetX = x - (_viewTilesX / 2) * 8; 00100 _offsetY = y - (_viewTilesY / 2) * 8; 00101 00102 int maxOffsetX = (_mapTilesX - _viewTilesX) * 8; 00103 int maxOffsetY = (_mapTilesY - _viewTilesY) * 8; 00104 00105 if (_offsetX < 0) _offsetX = 0; 00106 if (_offsetX > maxOffsetX) _offsetX = maxOffsetX; 00107 00108 if (_offsetY < 0) _offsetY = 0; 00109 if (_offsetY > maxOffsetY) _offsetY = maxOffsetY; 00110 } 00111 00112 bool TileViewer::canEnter(uint16_t x, uint16_t y) 00113 { 00114 const Block &block = getBlock(x, y); 00115 Block::Type type = block.getType(); 00116 00117 switch(type) 00118 { 00119 case Block::Background : return true; 00120 case Block::Solid : return false; 00121 } 00122 00123 return true; 00124 } 00125 00126 const Block& TileViewer::getBlock(uint16_t x, uint16_t y) 00127 { 00128 uint8_t tileX = x / 8; 00129 uint8_t tileY = y / 8; 00130 uint8_t blockId = _map[(tileY * _mapTilesX) + tileX]; 00131 return _blocks[blockId]; 00132 } 00133 00134 void TileViewer::animate(uint8_t spriteId) 00135 { 00136 Sprite &sprite = _sprites[spriteId]; 00137 sprite.animate(); 00138 } 00139 00140 bool TileViewer::pickupObject(uint8_t tileX, uint8_t tileY) 00141 { 00142 return false; 00143 } 00144 00145 void TileViewer::drawSprite(uint8_t spriteId, int16_t x, int16_t y) 00146 { 00147 const Sprite &sprite = _sprites[spriteId]; 00148 const ImageFrame &frame = sprite.getFrame(); 00149 00150 _canvas.drawBitmap(x, y, (Bitmap2bpp&)frame.getBitmap(), frame.getX(), frame.getY(), frame.getWidth(), frame.getHeight(), true); 00151 } 00152 00153 void TileViewer::update() 00154 { 00155 for (int i = 0; i < MAX_GAMEOBJECTS; ++i) 00156 { 00157 GameObject *p = _gameObjects[i]; 00158 if (p != NULL) 00159 { 00160 p->update(); 00161 } 00162 } 00163 00164 if (_pTrackedGameObject != NULL) 00165 { 00166 Point &position = _pTrackedGameObject->getPosition(); 00167 centerAt(position.X + 8, position.Y + 8); 00168 } 00169 } 00170 00171 void TileViewer::draw() 00172 { 00173 _canvas.clear(); 00174 00175 int firstTileX = max(0, _offsetX / 8); 00176 int firstTileY = max(0, _offsetY / 8); 00177 int lastTileX = min(_mapTilesX, firstTileX + _viewTilesX + 1); 00178 int lastTileY = min(_mapTilesY, firstTileY + _viewTilesY + 1); 00179 00180 int adjustX = _offsetX % 8; 00181 int adjustY = _offsetY % 8; 00182 00183 for (int y = -adjustY, yTile = firstTileY; yTile < lastTileY; ++yTile, y += 8) 00184 { 00185 int offset = yTile * _mapTilesX; 00186 for (int x = -adjustX, xTile = firstTileX; xTile < lastTileX; ++xTile, x += 8) 00187 { 00188 uint8_t blockId = _map[offset + xTile]; 00189 if (blockId == 0) continue; 00190 00191 const Block &block = _blocks[blockId]; 00192 const ImageFrame &frame = block.getFrame(); 00193 00194 _canvas.drawBitmap(x, y, (Bitmap2bpp&)frame.getBitmap(), 00195 frame.getX(), frame.getY(), 00196 frame.getWidth(), frame.getHeight(), false); 00197 } 00198 } 00199 00200 for (int i = 0; i < MAX_GAMEOBJECTS; ++i) 00201 { 00202 GameObject *p = _gameObjects[i]; 00203 if (p != NULL) 00204 { 00205 p->draw(); 00206 } 00207 } 00208 Game::Surface.drawBitmap(0, 0, _bitmap, 0, 0, _bitmap.getWidth(), _bitmap.getHeight()); 00209 }
Generated on Tue Jul 12 2022 21:15:25 by
