using System; using System.Collections; using System.Collections.Generic; using System.Linq; using Unity.Netcode; using UnityEngine; using UnityEngine.InputSystem; using UnityEngine.UIElements; namespace Game { public enum Side {Top, Bottom} public class Player : NetworkBehaviour { public Side Side { get; set; } protected bool goingLeft, goingRight; private VisualElement Panel { get; set; } private List Modifications { get; } = new(); private int Score { get; set; } // Units per second private const float BaseSpeed = 15; private const float SpeedMultiplier = 1.5f; protected float Speed { get { return Modifications.Where(m => m.effect == ModEffect.Speed) .Aggregate(BaseSpeed, (current, speedMod) => current * speedMod.type switch { ModType.Buff => SpeedMultiplier, ModType.Nerf => 1 / SpeedMultiplier, _ => throw new ArgumentOutOfRangeException() }); } } // Unit distance from zero private readonly float baseBorder = Dimensions.Singleton.PlaySize.x / 2 * 0.85f; private readonly float borderSummand = Dimensions.Singleton.PlaySize.x / 2 * 0.15f; protected float Border { get { return Mathf.Min(Modifications.Where(m => m.effect == ModEffect.Border) .Aggregate(baseBorder, (current, borderMod) => current + borderMod.type switch { ModType.Buff => borderSummand, ModType.Nerf => -borderSummand, _ => throw new ArgumentOutOfRangeException() }), Dimensions.Singleton.PlaySize.x / 2 ); } } protected float Width => transform.localScale.x; private float LeftEdge => X - Width / 2; private float RightEdge => X + Width / 2; protected float X => transform.position.x; protected float Y => transform.position.y; public void GainScore() { Score++; UpdatePanel(); } private void UpdatePanel() { Panel.Q("score").text = Score.ToString(); } public IEnumerator ProcessModification(ModificationProperties properties) { Modifications.Add(properties); VisualElement element = GameUI.AddModification(Panel, properties); float doNothingTime = properties.activeDuration * 0.7f; float animateTime = properties.activeDuration * 0.3f; yield return new WaitForSeconds(doNothingTime); float current = 0; while (current < animateTime) { current += Time.deltaTime; element.style.opacity = new StyleFloat(1 - current / animateTime); yield return null; } element.RemoveFromHierarchy(); Modifications.Remove(properties); } protected void ClampInsideBorders() { if (LeftEdge < -Border) transform.Translate(Vector2.right * (-Border - LeftEdge), Space.World); if (RightEdge > Border) transform.Translate(Vector2.left * (RightEdge - Border), Space.World); } protected void TryLinearMove(float h) { Vector2 trans = new Vector2((goingLeft ? -1 : 0) + (goingRight ? 1 : 0), 0); trans *= Speed * h; transform.Translate(trans, Space.World); ClampInsideBorders(); } protected void Start() { float y = Side switch { Side.Bottom => -Dimensions.Singleton.PlaySizeBoards.y / 2, Side.Top => Dimensions.Singleton.PlaySizeBoards.y / 2, _ => throw new ArgumentOutOfRangeException() }; transform.position = new Vector2(0, y); if (Side == Side.Top) transform.Rotate(transform.forward, 180); Panel = GameUI.Singleton.PlayerPanel(Side); UpdatePanel(); } } }