CyborgPlayer
public class CyborgPlayer : PlayerNetworkEntity, IDamage, IHittable
{
public sfloat moveSpeed = (sfloat)0.24f;
//back up data from server
public int currentAnimationMostRecentFromServer, currentFrameMostRecentFromServer, frameTimerMostRecentFromServer;
public int damage = 0;
public ObjectRect slashRectDown, slashRectUp, slashRectRight, slashRectLeft;
public sfloat dashAmount = (sfloat)1;
public int ammo = 3;
public bool haveExplodedThisDeath = false;
public GameObject explosionFXPrefab;
public NetworkEntityType bulletType;
//...
CyborgPlayer class extends PlayerNetworkEntity and is part of the sample project. It is recommended you create your own class that extends CyborgPlayer. This class is setup for 4 directional melee and shooting, so it covers probably a lot of the use cases of your own player controllers you have in mind. Feel free to chop and change this code or abandon it completely.
Methods
void Start()
This is a Unity method we're usign to make sure the PhysicsComponent is setup to be aware of our PlayerNetworkEntity's sTransform before we do anything else with them.
public void Update()
Another Unity method. DO NOT PUT any of your PlayerNetworkEntity logic in here as we're running NetworkEntities to a different timing to the Unity update thread. Mostly this method is used for helping lerping the NetworkEntity for those who do not control this PlayerNetworkEntity. You can generate special effects or particles here safely though. Basically anything you need replicated server and client side should not be here.
public override void UpdateEntity()
This is where your PlayerNetworkEntity gameplay logic can sit. At the moment its currently being used for updating the components it contains e.g hp, animation and physics.
public void UpdateAnimation()
A helper function built to help manage the animations of our PlayerNetworkEntity. Mostly this is managing what happens when particular animations end or if a certain frame of an animation is dealing damage or not.
public override void DoInput(PlayerInput input)
Takes in user input and attempts to do actions from them accordingly. For this example this is mostly walking around and attacking.
private void Walk(PlayerInput input)
Checks input and try’s to setup direction and instantVelocity which may move them during the physics part of the update.
private void Slash()
If the PlayerNetworkEntity is in a state ready to attack, change animation.
private void Shoot()
If the PlayerNetworkEntity is in a state ready to shoot, change animation and generate a bullet. Failing that, possibly reload.
Note: This is a good example of how a NetworkEntity can build other NetworkEntities and have them replicated across the network
private void Reload()
If the PlayerNetworkEntity is in a state ready to reload, change animation.
private void Dash()
If the PlayerNetworkEntity is in a state ready to dash, change animation and apply acceleration.
private void ApplyDashAcceleration()
depending on direction, setup localAcceleration so we can client side predict it and apply consistently on the server
public override void WriteToPacket(NetDataWriter writer)
Overridden from parent, however its been setup to defer its logic to another version of WriteToPacket
public void WriteToPacket(NetDataWriter writer, bool fullWrite)
Overridden to cover cases where we may either need just basic player info for the other players you see around you (usually just position and animation frame, or any other small bits) or a fullWrite of all the main attributes to send to the owning players from the server
public override void ReadFromPacket(NetDataReader reader)
Read information from the server about this PlayerNetworkEntity. If the packet suggests a fullWrite was done, we need to read in extra info, otherwise grab less data as less was written.
NOTE: Write and Read should mirror each other, except the Read should skip reading entityID and type as those have been read and consumed in the step beforehand to work out which NetworkEntity to pass the rest of the reading onto.
private void DoActionsAfterReadingPacket(int previousAnimation)
A space for managing logic after reading a packet from the server. Currently being used to back up data and play sounds
public int GetDamage()
returns damage attribute (at times its not zero to represent this frame can hurt others)
public HitType GetHitType()
Type of damage we're inflicting this frame. May be more complicated in your game as this character only does small hit types when damaging
public SRect GetDamageBox()
Represents the region where other things will get hurt by this NetworkEntity
public SVector GetAttackOrigin()
Currently using the sTransform position, but you may want it to be somewhere from within the hit area of the damageBox
public void HitSomething(IHittable hittable)
If we hit something, this method is called to respond to that fact
public SRect GetHitBox()
Returns the collision hit box for the PlayerNetworkEntity
public void TakeDamage(int dmg, HitType type, SVector attackOrigin, IDamage attacker)
Logic for dealing with incoming damage after being hit
public int GetCreatorEntityId()
return -1 means we are never considered being created by another NetworkEntity
public void KilledSomething(IHittable hittable)
Method for responding to the death of something else. May add score in this case
public bool IsHittable()
Yes/No, as we may be invincible temporarily after previously being hit
public List<ICollisionShape> GetDamageShapes()
Return regions as either rectangles or circles representing the areas where this PlayerNetworkEntity can hurt others
public List<ICollisionShape> GetHitShapes()
Return regions as either rectangles or circles representing the areas where this PlayerNetworkEntity can get hurt by others