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.
GameObject.cpp
00001 #include "mbed.h" 00002 #include "GameEngine.h" 00003 #include "GameObject.h" 00004 00005 GameObject::GameObject() : 00006 _spriteId(0), 00007 _position(0,0), 00008 _speed(2), 00009 _animationCounter(0), 00010 _collisionRect(0, 0, 16, 16) 00011 { 00012 00013 } 00014 00015 void GameObject::setPosition(const Point &position) 00016 { 00017 _position = position; 00018 } 00019 00020 void GameObject::setSpriteId(uint8_t spriteId) 00021 { 00022 _spriteId = spriteId; 00023 } 00024 00025 void GameObject::setSpeed(uint8_t speed) 00026 { 00027 _speed = speed; 00028 } 00029 00030 void GameObject::setCollisionRect(uint8_t x, uint8_t y, uint8_t w, uint8_t h) 00031 { 00032 _collisionRect = Rect(x, y, w, h); 00033 } 00034 00035 void GameObject::animate() 00036 { 00037 if ((_animationCounter++ % 2) == 0) 00038 { 00039 _pParent->animate(_spriteId); 00040 } 00041 } 00042 00043 bool GameObject::moveLeft() 00044 { 00045 if (!canGoLeft()) return false; 00046 _position.X -= _speed; 00047 return true; 00048 } 00049 00050 bool GameObject::moveRight() 00051 { 00052 if (!canGoRight()) return false; 00053 _position.X += _speed; 00054 return true; 00055 } 00056 00057 bool GameObject::moveUp() 00058 { 00059 if (!canGoUp()) return false; 00060 _position.Y -= _speed; 00061 return true; 00062 } 00063 00064 bool GameObject::moveDown() 00065 { 00066 if (!canGoDown()) return false; 00067 _position.Y += _speed; 00068 return true; 00069 } 00070 00071 const GameObject* GameObject::detectCollision() 00072 { 00073 return _pParent->detectCollision(this); 00074 } 00075 00076 bool GameObject::detectCollision(GameObject* other) 00077 { 00078 return _pParent->detectCollision(this, other); 00079 } 00080 00081 const Block* GameObject::detectBlock() 00082 { 00083 return _pParent->detectBlock(this); 00084 } 00085 00086 Rect GameObject::getCollisionRect() 00087 { 00088 return Rect(_position.X + _collisionRect.left, _position.Y + _collisionRect.top, _collisionRect.getWidth(), _collisionRect.getHeight()); 00089 } 00090 00091 bool GameObject::pickupObject() 00092 { 00093 uint8_t cellX = _position.X / 8; 00094 uint8_t cellY = _position.Y / 8; 00095 for (uint8_t cx = 0; cx < 2; ++cx) 00096 { 00097 for (uint8_t cy = 0; cy < 2; ++cy) 00098 { 00099 if (_pParent->pickupObject(cellX + cx, cellY + cy)) return true; 00100 } 00101 } 00102 00103 return false; 00104 } 00105 00106 bool GameObject::canGoLeft(uint16_t x, uint16_t y) 00107 { 00108 int16_t tx = x - _speed; 00109 if (tx < 0) return false; 00110 00111 if (!_pParent->canEnter(tx, y + 0)) return false; 00112 if (!_pParent->canEnter(tx, y + 7)) return false; 00113 if (!_pParent->canEnter(tx, y + 8)) return false; 00114 if (!_pParent->canEnter(tx, y + 15)) return false; 00115 00116 return true; 00117 } 00118 00119 bool GameObject::canGoRight(uint16_t x, uint16_t y) 00120 { 00121 int16_t tx = x + 15 + _speed; 00122 00123 if ((tx / 8) == _pParent->getMapTilesX()) return false; 00124 00125 if (!_pParent->canEnter(tx, y + 0)) return false; 00126 if (!_pParent->canEnter(tx, y + 7)) return false; 00127 if (!_pParent->canEnter(tx, y + 8)) return false; 00128 if (!_pParent->canEnter(tx, y + 15)) return false; 00129 00130 return true; 00131 } 00132 00133 bool GameObject::canGoUp(uint16_t x, uint16_t y) 00134 { 00135 int16_t ty = y - _speed; 00136 if (ty < 0) return false; 00137 00138 if (!_pParent->canEnter(x + 4, ty)) return false; 00139 if (!_pParent->canEnter(x + 7, ty)) return false; 00140 if (!_pParent->canEnter(x + 8, ty)) return false; 00141 if (!_pParent->canEnter(x + 11, ty)) return false; 00142 00143 return true; 00144 } 00145 00146 bool GameObject::canGoDown(uint16_t x, uint16_t y) 00147 { 00148 int16_t ty = y + 15 + _speed; 00149 00150 if ((ty / 8) == _pParent->getMapTilesY()) return false; 00151 00152 if (!_pParent->canEnter(x, ty)) return false; 00153 if (!_pParent->canEnter(x + 7, ty)) return false; 00154 if (!_pParent->canEnter(x + 8, ty)) return false; 00155 if (!_pParent->canEnter(x + 15, ty)) return false; 00156 00157 return true; 00158 } 00159 00160 void GameObject::update() 00161 { 00162 animate(); 00163 } 00164 00165 void GameObject::draw() 00166 { 00167 _pParent->drawSprite(_spriteId, _position.X - _pParent->getOffsetX(), _position.Y - _pParent->getOffsetY()); 00168 }
Generated on Tue Jul 12 2022 21:15:25 by
