3 simple and practical RPG Algorithm Based on cocos2dx, cocos2dxrpg
1. Determine a formation center object.
It may be a hero or a hidden object. That is, the GridCenter of the following text
2. Pre-calculate the vector of the relative Center Object of each array "slot.
Void GameControlManager: startGridMode ()
{
If (m_MainScene-> heroList. empty ())
Return;
M_IsStartGridMode = true;
Point GridCenter = findGridCenter ();
If (memberNumber = 1)
Return;
Else if (memberNumber = 2)
{
// 0
// 1
OriginRelativeVec [0] = Vec2 (0, Grid_Slot_Radius );
OriginRelativeVec [1] = Vec2 (0,-Grid_Slot_Radius );
}
Else if (memberNumber = 3)
{
Point firstPos = GridCenter + Vec2 (0, Grid_Slot_Radius );
Point secondPos = firstPos. rotateByAngle (GridCenter, CC_DEGREES_TO_RADIANS (-120 ));
Point thirdPos = secondPos. rotateByAngle (GridCenter, CC_DEGREES_TO_RADIANS (-120 ));
// 0
//
// 1 2
OriginRelativeVec [0] = firstPos-GridCenter;
OriginRelativeVec [1] = secondPos-GridCenter;
OriginRelativeVec [2] = thirdPos-GridCenter;
}
Else if (memberNumber = 4)
{
Point firstPos = GridCenter + Vec2 (0, Grid_Slot_Radius );
Point secondPos = firstPos. rotateByAngle (GridCenter, CC_DEGREES_TO_RADIANS (-90 ));
Point thirdPos = secondPos. rotateByAngle (GridCenter, CC_DEGREES_TO_RADIANS (-90 ));
Point fourthPos = thirdPos. rotateByAngle (GridCenter, CC_DEGREES_TO_RADIANS (-90 ));
// 0 warriors
// 1 2 hunter mage
// 3 priest
OriginRelativeVec [0] = firstPos-GridCenter;
OriginRelativeVec [1] = fourthPos-GridCenter;
OriginRelativeVec [2] = secondPos-GridCenter;
OriginRelativeVec [3] = thirdPos-GridCenter;
}
// Claim the slot location
Int slotIndex = 0;
Int minspeed= 999;
For (auto hero: m_MainScene-> heroList) // sorted
{
If (! Hero-> getIsAlly ()&&! Hero-> getIsDead ())
{
Hero-> setSlotIndex (slotIndex );
SlotIndex ++;
Auto actorInfo = GameData: getActorInfoFromMap (hero-> getUnitID ());
If (actorInfo-> speed <minSpeed)
MinSpeed = actorInfo-> speed;
}
}
3. determine the initial orientation of the formation based on the relative Center Object direction of hero 1.
Point firstmanPos = m_MainScene-> heroList. front ()-> getCenterPoint ();
// Crossover_point (firstmanPos, GridCenter ,)
Vec2 heroVec = firstmanPos-GridCenter;
HeroVec. normalize ();
M_GridAngle = getDirectionByChief (heroVec );
For (int index = 0; index <memberNumber; index ++)
{
Vec2 cur = originRelativeVec [index];
Point curPoint = cur + GridCenter;
CurPoint = curPoint. rotateByAngle (GridCenter, m_GridAngle );
Cur = curPoint-GridCenter;
SlotRelativeVec [index] = cur;
}
4. All heroes are here.
For (auto hero: m_MainScene-> heroList)
{
If (! Hero-> getIsAlly ()&&! Hero-> getIsDead ())
{
Vec2 curVec = slotRelativeVec [hero-> getSlotIndex ()];
Point des = GridCenter + curVec;
Hero-> setDestinationPoint (des );
}
}
....
}
5. When the formations move, the positional orientation angle GridAngle is updated based on the target location of the central object targetPos.
Refresh the relative vector cur of each slot based on GridAngle.
Void GameControlManager: setGridDirection (Point targetPos)
{
// Update the positional Orientation
Auto GridCenter = getGridCenter ();
Vec2 chiefVec = targetPos-GridCenter;
ChiefVec. normalize ();
M_GridAngle = getDirectionByChief (chiefVec );
For (int index = 0; index <memberNumber; index ++)
{
Vec2 cur = originRelativeVec [index];
Point curPoint = cur + m_gridObject-> getPosition ();
CurPoint = curPoint. rotateByAngle (m_gridObject-> getPosition (), m_GridAngle); // rotate on the original base
Cur = curPoint-m_gridObject-> getPosition ();
SlotRelativeVec [index] = cur;
}
M_gridObject-> setRotation (CC_RADIANS_TO_DEGREES (-m_GridAngle ));
}
6. Move the role to its own slot at each frame.
Void GameControlManager: updateGridDirection ()
{
Point slot = getSlotPosByIndex (hero-> getSlotIndex ());
If (hero-> getCenterPoint (). distance (slot)> getElasticRange ())
{
Hero-> moveToward (slot );
}
}
Copyright Disclaimer: This article is an original article by the blogger and cannot be reproduced without the permission of the blogger.