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.
 
 
 

76 lines
1.6 KiB

using System;
using Unity.Netcode;
using UnityEngine;
using UnityEngine.InputSystem;
namespace Game {
public class Player : NetworkBehaviour {
public enum ESide {Top, Bottom}
public ESide Side { get; set; }
private int score;
protected bool goingLeft, goingRight;
// Units per second
protected float Speed => 10;
// Unit distance from zero
private float Border => 10;
private SpeedModification speedModification;
private BorderModification borderModification;
private float LeftSide() {
return X() - transform.localScale.x / 2;
}
private float RightSide() {
return X() + transform.localScale.x / 2;
}
protected float X() {
return transform.position.x;
}
protected float Y() {
return transform.position.y;
}
protected void TryMove(float h) {
Vector2 trans = new Vector2((goingLeft ? -1 : 0) + (goingRight ? 1 : 0), 0);
trans *= Speed * h;
transform.Translate(trans);
if (LeftSide() < -Border)
transform.Translate(Vector2.right * (-Border - LeftSide()));
if (RightSide() > Border)
transform.Translate(Vector2.left * (RightSide() - Border));
}
private void Start() {
float y = Side switch {
ESide.Bottom => BorderSize.Singleton.y1,
ESide.Top => BorderSize.Singleton.y2,
_ => throw new ArgumentOutOfRangeException()
};
transform.position = new Vector2(0, y);
}
}
public class RealPlayer : Player {
public bool isThisClient;
private void FixedUpdate() {
if (!isThisClient)
return;
var keyboard = Keyboard.current;
goingLeft = keyboard.aKey.isPressed;
goingRight = keyboard.dKey.isPressed;
TryMove(Time.fixedDeltaTime);
}
}
}