Game Development Reference
In-Depth Information
Initially, check that the destination vector does not have a difference greater than
the
kTankSpeed
. Then, update the position by the
tankStatsForLocal
destination and if the difference is greater, then write the code for turning around;
that is, calculate the angle difference between the tank direction and
tankRota-
tion
.
8. Check if the difference is greater than
kTankTurnSpeed
, then find the closest
180 degrees to be rotated, and according to that subtract or add the
kTankTurnSpeed
with the rotation. If the difference is not greater than the fa-
cing, move around the line towards the destination. Set the rotation as the direc-
tion and calculate the position of the tank using current position, destination, and
kTankSpeed
.
All these calculations should be assigned to the
tankStatsForLocaldata
structure. After all this, set the
tankPreviousPosition
of the local data
structure as the current position of the local player sprite. Update the position and
rotation calculated in the
tankStatsForLocalstructure
. To sync the
player's movement produced in this method, we need to send a packet to the other
player with
NetworkPacketCode
as
KNetworkPacketCodePlayer-
Move
data part will be in the structure of
tankStatsForLocal
and this pack-
et should be sent unreliably because it is been sent very frequently.
9. In the delegate method of
MCSessiondidReceiveData
, the packet for the
movement of the player with type
KNetworkPacketCodePlayerMove
is re-
ceived.
case KNetworkPacketCodePlayerMove:
{
// received move event from other player, update
other player's position/destination info
TankInfo *ts = (TankInfo *)&incomingPacket[8];
self.remoteTankSprite.position = ts->tankPosition;
self.remoteTankSprite.zRotation = ts->tankRotation;
break;
}
10. Here,
TankInfo
is the structure that holds all the location and rotation related
data of the user. The
TankInfo
structure is sent over the network to sync both
the devices.