You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

81 lines
1.8 KiB

using System;
using Unity.Netcode;
using UnityEngine;
using UnityEngine.InputSystem;
namespace Game {
2 years ago
public enum Side {Top, Bottom}
public class Player : NetworkBehaviour {
2 years ago
2 years ago
public Side Side { get; set; }
private int score;
2 years ago
protected bool goingLeft, goingRight;
// Units per second
2 years ago
protected float Speed => 15;
2 years ago
// Unit distance from zero
protected float Border => 10;
private SpeedModification speedModification;
private BorderModification borderModification;
protected float Width => transform.localScale.x;
2 years ago
private float LeftEdge => X() - Width / 2;
private float RightEdge => X() + Width / 2;
protected float X() {
return transform.position.x;
}
2 years ago
protected float Y() {
return transform.position.y;
}
protected void ClampInsideBorders() {
2 years ago
if (LeftEdge < -Border)
transform.Translate(Vector2.right * (-Border - LeftEdge), Space.World);
if (RightEdge > Border)
transform.Translate(Vector2.left * (RightEdge - Border), Space.World);
2 years ago
}
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();
}
2 years ago
private void Start() {
float y = Side switch {
2 years ago
Side.Bottom => BorderSize.Singleton.y1,
Side.Top => BorderSize.Singleton.y2,
2 years ago
_ => throw new ArgumentOutOfRangeException()
};
transform.position = new Vector2(0, y);
2 years ago
if (Side == Side.Top)
transform.Rotate(transform.forward, 180);
}
}
2 years ago
public class RealPlayer : Player {
2 years ago
public bool isThisClient;
private void FixedUpdate() {
2 years ago
if (!isThisClient)
return;
var keyboard = Keyboard.current;
2 years ago
goingLeft = keyboard.aKey.isPressed;
goingRight = keyboard.dKey.isPressed;
TryLinearMove(Time.fixedDeltaTime);
}
}
}