123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100 |
- using System.Collections.Generic;
- using System.Numerics;
- using System.Text.Json;
- using BubbleSocketCore.Input;
- namespace BubbleSocketCore.Simulation.Entity
- {
- public class PlayerEntity : IEntity
- {
- private int id;
- private int connectionHashCode;
- private string color;
- private Vector3 position = new Vector3(20, 20, 0);
- private Vector3 velocity = new Vector3(0, 0, 0);
- private Vector3 acceleration = new Vector3(0, 0, 0);
- private float speed = 0.1f;
- public PlayerEntity(int hashCode, string playerColor)
- {
- connectionHashCode = hashCode;
- color = playerColor;
- }
- public int GetId()
- {
- return id;
- }
- public void SetId(int entityId)
- {
- id = entityId;
- }
- public Dictionary<string, object> Serialize()
- {
- return new Dictionary<string, object> {
- {"id", id},
- {"color", color},
- {"position", ToList(position)},
- {"velocity", ToList(velocity)},
- {"acceleration", ToList(acceleration)},
- {"speed", speed}
- };
- }
- private List<object> ToList(Vector3 vector)
- {
- return new List<object> {
- vector.X,
- vector.Y,
- vector.Z
- };
- }
- override public string ToString()
- {
- return JsonSerializer.Serialize(Serialize());
- }
- public IEntity ApplyInputs(int elapsedTick, List<PlayerInput> inputs)
- {
- var playerClone = (PlayerEntity)Clone();
- inputs.ForEach(input =>
- {
- switch ((InputList)input.type)
- {
- case InputList.UP:
- playerClone.acceleration.Y = input.value == 1 ? -1 * playerClone.speed : 0;
- break;
- case InputList.DOWN:
- playerClone.acceleration.Y = input.value == 1 ? 1 * playerClone.speed : 0;
- break;
- case InputList.LEFT:
- playerClone.acceleration.X = input.value == 1 ? -1 * playerClone.speed : 0;
- break;
- case InputList.RIGHT:
- playerClone.acceleration.X = input.value == 1 ? 1 * playerClone.speed : 0;
- break;
- }
- });
- return playerClone;
- }
- public IEntity Clone()
- {
- var clonedPlayer = new PlayerEntity(connectionHashCode, color);
- clonedPlayer.id = id;
- clonedPlayer.acceleration.X = acceleration.X;
- clonedPlayer.acceleration.Y = acceleration.Y;
- clonedPlayer.acceleration.Z = acceleration.Z;
- clonedPlayer.velocity.X = velocity.X;
- clonedPlayer.velocity.Y = velocity.Y;
- clonedPlayer.velocity.Z = velocity.Z;
- clonedPlayer.position.X = position.X;
- clonedPlayer.position.Y = position.Y;
- clonedPlayer.position.Z = position.Z;
- clonedPlayer.speed = speed;
- return clonedPlayer;
- }
- }
- }
|