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 Serialize() { return new Dictionary { {"id", id}, {"color", color}, {"position", ToList(position)}, {"velocity", ToList(velocity)}, {"acceleration", ToList(acceleration)}, {"speed", speed} }; } private List ToList(Vector3 vector) { return new List { vector.X, vector.Y, vector.Z }; } override public string ToString() { return JsonSerializer.Serialize(Serialize()); } public IEntity ApplyInputs(int elapsedTick, List 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; } } }