main
Benjamin Kraft 2 years ago
parent 15b7271ee1
commit 644e9f3418
  1. 13
      Assets/Scripts/Game/AIPlayer.cs
  2. 6
      Assets/Scripts/Game/GameManager.cs
  3. 28
      Assets/Scripts/Game/Player.cs

@ -14,8 +14,8 @@ namespace Game {
private bool BallApproaches(Ball ball) { private bool BallApproaches(Ball ball) {
var ballVy = ball.Rb.velocity.y; var ballVy = ball.Rb.velocity.y;
return Side switch { return Side switch {
ESide.Bottom => ballVy < 0, Side.Bottom => ballVy < 0,
ESide.Top => ballVy > 0, Side.Top => ballVy > 0,
_ => throw new Exception("Side not set on player!") _ => throw new Exception("Side not set on player!")
}; };
} }
@ -91,7 +91,7 @@ namespace Game {
if (area.Contains(end)) if (area.Contains(end))
return end.x; return end.x;
float playerY = Side == ESide.Bottom ? area.yMin : area.yMax; float playerY = Side == Side.Bottom ? area.yMin : area.yMax;
Vector2 playerLeft = new Vector2(area.xMin, playerY); Vector2 playerLeft = new Vector2(area.xMin, playerY);
Vector2 playerRight = new Vector2(area.xMax, playerY); Vector2 playerRight = new Vector2(area.xMax, playerY);
@ -161,7 +161,7 @@ namespace Game {
private float currentSmoothV; private float currentSmoothV;
private void ApproachPosition(float pos) { private void ApproachPosition(float pos) {
float result = Mathf.SmoothDamp(X(), pos, ref currentSmoothV, .5f); float result = Mathf.SmoothDamp(X(), pos, ref currentSmoothV, .5f, Speed);
transform.position = new Vector2(result, Y()); transform.position = new Vector2(result, Y());
ClampInsideBorders(); ClampInsideBorders();
} }
@ -169,14 +169,15 @@ namespace Game {
private bool isApproaching; private bool isApproaching;
private float lastDirection; private float lastDirection;
private void FixedUpdate() { private void FixedUpdate() {
float dt = Time.fixedDeltaTime;
float target = GetTargetPosition(); float target = GetTargetPosition();
float h = Width / 2; float h = Mathf.Max(Speed * dt, Width / 2);
goingLeft = target < X() - h; goingLeft = target < X() - h;
goingRight = target > X() + h; goingRight = target > X() + h;
if (goingLeft || goingRight) { if (goingLeft || goingRight) {
isApproaching = false; isApproaching = false;
lastDirection = goingLeft ? -1 : 1; lastDirection = goingLeft ? -1 : 1;
TryLinearMove(Time.fixedDeltaTime); TryLinearMove(dt);
} }
else { else {
if (!isApproaching) { if (!isApproaching) {

@ -53,7 +53,7 @@ namespace Game {
} }
private void Start() { private void Start() {
Settings.Type = Type.Hybrid; Settings.Type = Type.AI;
Settings.AIDifficulty = Difficulty.VeryHard; Settings.AIDifficulty = Difficulty.VeryHard;
var ball = Instantiate(ballPrefab).GetComponent<Ball>(); var ball = Instantiate(ballPrefab).GetComponent<Ball>();
@ -85,8 +85,8 @@ namespace Game {
default: default:
throw new ArgumentOutOfRangeException(); throw new ArgumentOutOfRangeException();
} }
p1.Side = Player.ESide.Bottom; p1.Side = Side.Bottom;
p2.Side = Player.ESide.Top; p2.Side = Side.Top;
Tests(); Tests();
} }

@ -4,11 +4,12 @@ using UnityEngine;
using UnityEngine.InputSystem; using UnityEngine.InputSystem;
namespace Game { namespace Game {
public class Player : NetworkBehaviour {
public enum ESide {Top, Bottom} public enum Side {Top, Bottom}
public class Player : NetworkBehaviour {
public ESide Side { get; set; } public Side Side { get; set; }
private int score; private int score;
@ -18,15 +19,15 @@ namespace Game {
protected float Speed => 10; protected float Speed => 10;
// Unit distance from zero // Unit distance from zero
private float Border => 7; private float Border => 10;
private SpeedModification speedModification; private SpeedModification speedModification;
private BorderModification borderModification; private BorderModification borderModification;
protected float Width => transform.localScale.x; protected float Width => transform.localScale.x;
private float LeftSide => X() - Width / 2; private float LeftEdge => X() - Width / 2;
private float RightSide => X() + Width / 2; private float RightEdge => X() + Width / 2;
protected float X() { protected float X() {
return transform.position.x; return transform.position.x;
@ -37,10 +38,10 @@ namespace Game {
} }
protected void ClampInsideBorders() { protected void ClampInsideBorders() {
if (LeftSide < -Border) if (LeftEdge < -Border)
transform.Translate(Vector2.right * (-Border - LeftSide), Space.World); transform.Translate(Vector2.right * (-Border - LeftEdge), Space.World);
if (RightSide > Border) if (RightEdge > Border)
transform.Translate(Vector2.left * (RightSide - Border), Space.World); transform.Translate(Vector2.left * (RightEdge - Border), Space.World);
} }
protected void TryLinearMove(float h) { protected void TryLinearMove(float h) {
@ -52,16 +53,15 @@ namespace Game {
private void Start() { private void Start() {
float y = Side switch { float y = Side switch {
ESide.Bottom => BorderSize.Singleton.y1, Side.Bottom => BorderSize.Singleton.y1,
ESide.Top => BorderSize.Singleton.y2, Side.Top => BorderSize.Singleton.y2,
_ => throw new ArgumentOutOfRangeException() _ => throw new ArgumentOutOfRangeException()
}; };
transform.position = new Vector2(0, y); transform.position = new Vector2(0, y);
if (Side == ESide.Top) { if (Side == Side.Top)
transform.Rotate(transform.forward, 180); transform.Rotate(transform.forward, 180);
} }
} }
}
public class RealPlayer : Player { public class RealPlayer : Player {
public bool isThisClient; public bool isThisClient;

Loading…
Cancel
Save