30#include "ResourceManager.h"
31#include "BrainsFactory.h"
35Object::Object(Sint32 iObjID, Sint32 iMX, Sint32 iMY, GameField* GF)
39 Main& mC64 = Main::Instance();
44 iPositionX = iStartingPositionX = iMX * MAZE_TILE_SIZE;
45 iPositionY = iStartingPositionY = iMY * MAZE_TILE_SIZE;
46 iMazeX = iMazePrevX = iMX;
47 iMazeY = iMazePrevY = iMY;
48 iMazeRenderingX = iMazeRenderingY = 0;
49 bRenderExecuteSync =
false;
57 case PME_OBJECT_PACMAN:
58 sprID = ResourceManager::Instance().get(RM_SPR_PACMAN);
60 case PME_OBJECT_GHOST_RED:
61 sprID = ResourceManager::Instance().get(RM_SPR_GHOSTRED);
63 case PME_OBJECT_GHOST_PINK:
64 sprID = ResourceManager::Instance().get(RM_SPR_GHOSTPINK);
66 case PME_OBJECT_GHOST_BLUE:
67 sprID = ResourceManager::Instance().get(RM_SPR_GHOSTBLUE);
69 case PME_OBJECT_GHOST_ORANGE:
70 sprID = ResourceManager::Instance().get(RM_SPR_GHOSTORANGE);
72 case PME_OBJECT_PELLET_POWER1:
73 case PME_OBJECT_PELLET_POWER2:
74 case PME_OBJECT_PELLET_POWER3:
75 case PME_OBJECT_PELLET_POWER4:
76 sprID = ResourceManager::Instance().get(RM_SPR_PELLETPOWER);
78 sprObj = mC64.ISpriteMgr().get(sprID);
79 j = mC64.ITool().randWELL() % 10;
80 fSpeed = (float)j / 100.0f;
82 sprObj->setSpeed(fSpeed);
84 mC64.ILogMgr().get()->msg(LML_NORMAL,
" [Object] Power Pellet speed set to '%.2f'.\n", fSpeed);
88 mC64.ILogMgr().get()->msg(LML_NORMAL,
" [Object] Unknown object id '%d'\n", iObjID);
92 Main::Instance().ILogMgr().get()->msg(LML_LOW,
" [Object] Object ID '%d' created.\n", iID);
99 if(sprID != 0) Main::Instance().ISpriteMgr().close(sprID);
101 pGameField =
nullptr;
102 #ifdef DEBUG_INTERNAL
103 Main::Instance().ILogMgr().get()->msg(LML_LOW,
" [Object] Object ID '%d' deleted.\n", iID);
108Sint32 Object::execute()
110 if(bRenderExecuteSync)
112 Main::Instance().ISpriteMgr().get(sprID)->setPosition(iMazeRenderingX + iPositionX, iMazeRenderingY + iPositionY, 1);
118Sint32 Object::render(Sint32 iSX, Sint32 iSY)
120 iMazeRenderingX = iSX;
121 iMazeRenderingY = iSY;
122 if(!bRenderExecuteSync)
124 bRenderExecuteSync =
true;
127 if(sprID != 0) Main::Instance().ISpriteMgr().get(sprID)->render();
132Sint32 Object::debug(Sint32 iMode)
138Sint32 Object::msgPause()
144Sint32 Object::msgGoInit()
146 iPositionX = iStartingPositionX;
147 iPositionY = iStartingPositionY;
148 iMazeX = iPositionX / MAZE_TILE_SIZE;
149 iMazeY = iPositionY / MAZE_TILE_SIZE;
154Sint32 Object::msgPelletPowerEaten(Sint32 iX, Sint32 iY)
160Sint32 Object::msgGhostCollision()
166Sint32 Object::getID()
172void Object::getName(
string &sN)
176 case PME_OBJECT_PACMAN:
179 case PME_OBJECT_GHOST_RED:
182 case PME_OBJECT_GHOST_PINK:
185 case PME_OBJECT_GHOST_BLUE:
188 case PME_OBJECT_GHOST_ORANGE:
191 case PME_OBJECT_PELLET_POWER1:
194 case PME_OBJECT_PELLET_POWER2:
197 case PME_OBJECT_PELLET_POWER3:
200 case PME_OBJECT_PELLET_POWER4:
207void Object::getPositionMaze(Sint32 &iX, Sint32 &iY)
214void Object::getDirection(Sint32 &iX, Sint32 &iY)
222 Main::Instance().ISpriteMgr().get(sprID)->getAnimState(-1, &iState);
223 iState = SPR_GET_HIGHSTATE(iState);
225 if(iState == SPR_STATE_UP) --iY;
226 else if(iState == SPR_STATE_DOWN) ++iY;
227 else if(iState == SPR_STATE_LEFT) --iX;
228 else if(iState == SPR_STATE_RIGHT) ++iX;
232State::State(
const string& sN)
239void State::enter(Actor* pObj)
243 pObj->getName(sObjName);
244 Main::Instance().ILogMgr().get()->msg(LML_INFO,
"[%d] '%s' enters '%s' state\n", Main::Instance().ITimer().getTicksNow(), sObjName.c_str(), sName.c_str());
248void State::exit(Actor* pObj)
252 pObj->getName(sObjName);
253 Main::Instance().ILogMgr().get()->msg(LML_INFO,
"[%d] '%s' exits '%s' state\n", Main::Instance().ITimer().getTicksNow(), sObjName.c_str(), sName.c_str());
257void State::getName(
string& sN)
265Actor::Actor(Sint32 iObjID, Sint32 iMX, Sint32 iMY, GameField* GF) : Object(iObjID, iMX, iMY, GF)
274 pointNextStep.iX = pointNextStep.iY = 0;
275 pointTarget.iX = pointTarget.iY = 0;
280 case PME_OBJECT_PACMAN:
281 sprTargetID = ResourceManager::Instance().get(RM_SPR_TARGETS);
282 sprObj = Main::Instance().ISpriteMgr().get(sprTargetID);
283 sprObj->selectAnim(4);
285 case PME_OBJECT_GHOST_RED:
286 sprTargetID = ResourceManager::Instance().get(RM_SPR_TARGETS);
287 sprObj = Main::Instance().ISpriteMgr().get(sprTargetID);
288 sprObj->selectAnim(0);
290 case PME_OBJECT_GHOST_PINK:
291 sprTargetID = ResourceManager::Instance().get(RM_SPR_TARGETS);
292 sprObj = Main::Instance().ISpriteMgr().get(sprTargetID);
293 sprObj->selectAnim(1);
295 case PME_OBJECT_GHOST_BLUE:
296 sprTargetID = ResourceManager::Instance().get(RM_SPR_TARGETS);
297 sprObj = Main::Instance().ISpriteMgr().get(sprTargetID);
298 sprObj->selectAnim(2);
300 case PME_OBJECT_GHOST_ORANGE:
301 sprTargetID = ResourceManager::Instance().get(RM_SPR_TARGETS);
302 sprObj = Main::Instance().ISpriteMgr().get(sprTargetID);
303 sprObj->selectAnim(3);
309 pCurrentState =
nullptr;
310 pPreviousState =
nullptr;
313 #ifdef DEBUG_INTERNAL
314 Main::Instance().ILogMgr().get()->msg(LML_LOW,
" [Actor] Actor ID '%d' created.\n", iID);
321 pCurrentState =
nullptr;
322 pPreviousState =
nullptr;
324 if(sprTargetID != 0) Main::Instance().ISpriteMgr().close(sprTargetID);
330 #ifdef DEBUG_INTERNAL
331 Main::Instance().ILogMgr().get()->msg(LML_LOW,
" [Actor] Actor ID '%d' deleted.\n", iID);
336Sint32 Actor::execute()
341 if(idBrain == -1)
return -1;
347 if(eAM != AM_DISABLED && vPath.size() > 0)
353 if(eAM == AM_NEXT_STEP)
355 pointNextStep = vPath.back();
360 if(eAM == AM_WORKING)
363 if(iPositionX < (-(MAZE_TILE_SIZE / 2)))
365 pGameField->moveTo(iID, iMazeX, iMazeY, MAZE_WIDTH - 1, iMazeY);
366 iMazeX = MAZE_WIDTH - 1;
367 iPositionX = (iMazeX * MAZE_TILE_SIZE) + (MAZE_TILE_SIZE / 2);
368 #ifdef DEBUG_INTERNAL
369 Main::Instance().ILogMgr().get()->msg(LML_LOW,
"tunel left\n");
371 pointNextStep.iX = iMazeX;
374 else if(iPositionX >((MAZE_WIDTH - 1) * MAZE_TILE_SIZE) + (MAZE_TILE_SIZE / 2))
376 pGameField->moveTo(iID, iMazeX, iMazeY, 0, iMazeY);
378 iPositionX = -(MAZE_TILE_SIZE / 2);
379 #ifdef DEBUG_INTERNAL
380 Main::Instance().ILogMgr().get()->msg(LML_LOW,
"tunel right\n");
382 pointNextStep.iX = iMazeX;
386 if((pointNextStep.iY * MAZE_TILE_SIZE) > iPositionY)
390 if((pointNextStep.iY * MAZE_TILE_SIZE) < iPositionY)
392 iPositionY = pointNextStep.iY * MAZE_TILE_SIZE;
393 #ifdef DEBUG_INTERNAL
394 Main::Instance().ILogMgr().get()->msg(LML_LOW,
"Y+ overstep!\n");
398 else if((pointNextStep.iY * MAZE_TILE_SIZE) < iPositionY)
402 if((pointNextStep.iY * MAZE_TILE_SIZE) > iPositionY)
404 iPositionY = pointNextStep.iY * MAZE_TILE_SIZE;
405 #ifdef DEBUG_INTERNAL
406 Main::Instance().ILogMgr().get()->msg(LML_LOW,
"Y- overstep!\n");
412 else if((pointNextStep.iX * MAZE_TILE_SIZE) > iPositionX)
416 if((pointNextStep.iX * MAZE_TILE_SIZE) < iPositionX)
418 iPositionX = pointNextStep.iX * MAZE_TILE_SIZE;
419 #ifdef DEBUG_INTERNAL
420 Main::Instance().ILogMgr().get()->msg(LML_LOW,
"X+ overstep!\n");
424 else if((pointNextStep.iX * MAZE_TILE_SIZE) < iPositionX)
428 if((pointNextStep.iX * MAZE_TILE_SIZE) > iPositionX)
430 iPositionX = pointNextStep.iX * MAZE_TILE_SIZE;
431 #ifdef DEBUG_INTERNAL
432 Main::Instance().ILogMgr().get()->msg(LML_LOW,
"X- overstep!\n");
446 if(iTmpX != iMazeX || iTmpY != iMazeY)
459Sint32 Actor::debug(Sint32 iMode)
464 Sprite* sprObj = Main::Instance().ISpriteMgr().get(sprTargetID);
465 if(sprObj !=
nullptr)
467 Sint32 iX = pointTarget.iX;
468 Sint32 iY = pointTarget.iY;
472 else if(iX > MAZE_WIDTH) iX = MAZE_WIDTH - 1;
474 else if(iY > MAZE_HEIGHT) iY = MAZE_HEIGHT - 1;
477 sprObj->setPosition(iMazeRenderingX + iX * MAZE_TILE_SIZE, iMazeRenderingY + iY * MAZE_TILE_SIZE, 0);
485Sint32 Actor::msgPause()
493Sint32 Actor::msgGoInit()
497 Main::Instance().ISpriteMgr().get(sprID)->selectAnim(SPR_STATE_LEFT);
502double Actor::euclideanDistance(Sint32 iTX, Sint32 iTY, MazePoint& vMP)
505 iDX = abs(vMP.iX - iTX);
506 iDY = abs(vMP.iY - iTY);
507 vMP.dDistance = sqrt((iDX * iDX) + (iDY * iDY));
508 return vMP.dDistance;
512Sint32 Actor::getStateName(
string &sN)
514 pCurrentState->getName(sN);
519Sint32 Actor::getBrain()
525Sint32 Actor::setBrain(Sint32 idB)
528 if((idB & PME_BRAIN_TYPE_RANDOM) == PME_BRAIN_TYPE_RANDOM) idB = PME_BRAIN_TYPE_RANDOM;
529 if(BrainsFactory::Instance().validate(idB) ==
true)
538Sint32 Actor::think(Sint32& iTX, Sint32& iTY)
542 return BrainsFactory::Instance().think(
this, idBrain, iTX, iTY, pGameField);
547Sint32 Actor::moveX(Sint32 iDir)
549 Sprite* sprObj = Main::Instance().ISpriteMgr().get(sprID);
555 iPositionX += iSpeed;
556 if(((iPositionX + (MAZE_TILE_SIZE / 2)) / MAZE_TILE_SIZE) != iMazeX)
560 pGameField->moveTo(iID, iMazeX - 1, iMazeY, iMazeX, iMazeY);
564 sprObj->selectAnim(SPR_STATE_RIGHT + eSS);
571 iPositionX -= iSpeed;
572 if(((iPositionX + (MAZE_TILE_SIZE / 2)) / MAZE_TILE_SIZE) != iMazeX)
576 pGameField->moveTo(iID, iMazeX + 1, iMazeY, iMazeX, iMazeY);
580 sprObj->selectAnim(SPR_STATE_LEFT + eSS);
585Sint32 Actor::moveY(Sint32 iDir)
587 Sprite* sprObj = Main::Instance().ISpriteMgr().get(sprID);
593 iPositionY += iSpeed;
594 if(((iPositionY + (MAZE_TILE_SIZE / 2)) / MAZE_TILE_SIZE) != iMazeY)
598 pGameField->moveTo(iID, iMazeX, iMazeY - 1, iMazeX, iMazeY);
602 sprObj->selectAnim(SPR_STATE_DOWN + eSS);
609 iPositionY -= iSpeed;
610 if(((iPositionY + (MAZE_TILE_SIZE / 2)) / MAZE_TILE_SIZE) != iMazeY)
614 pGameField->moveTo(iID, iMazeX, iMazeY + 1, iMazeX, iMazeY);
618 sprObj->selectAnim(SPR_STATE_UP + eSS);
624void Actor::stateChange(State* pNewState)
627 if(!pNewState)
return;
630 if(pNewState != pCurrentState) pPreviousState = pCurrentState;
633 if(pCurrentState) pCurrentState->exit(
this);
636 pCurrentState = pNewState;
639 pCurrentState->enter(
this);
643void Actor::statePrevious()
645 stateChange(pPreviousState);
649bool Actor::stateCurrentCheck(State& pState)
651 return typeid(*pCurrentState) ==
typeid(pState);
655bool Actor::statePreviousCheck(State& pState)
657 if(pPreviousState ==
nullptr)
return false;
658 return typeid(*pPreviousState) ==
typeid(pState);