main
Benjamin Kraft 1 year 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) {
var ballVy = ball.Rb.velocity.y;
return Side switch {
ESide.Bottom => ballVy < 0,
ESide.Top => ballVy > 0,
Side.Bottom => ballVy < 0,
Side.Top => ballVy > 0,
_ => throw new Exception("Side not set on player!")
};
}
@ -91,7 +91,7 @@ namespace Game {
if (area.Contains(end))
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 playerRight = new Vector2(area.xMax, playerY);
@ -161,7 +161,7 @@ namespace Game {
private float currentSmoothV;
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());
ClampInsideBorders();
}
@ -169,14 +169,15 @@ namespace Game {
private bool isApproaching;
private float lastDirection;
private void FixedUpdate() {
float dt = Time.fixedDeltaTime;
float target = GetTargetPosition();
float h = Width / 2;
float h = Mathf.Max(Speed * dt, Width / 2);
goingLeft = target < X() - h;
goingRight = target > X() + h;
if (goingLeft || goingRight) {
isApproaching = false;
lastDirection = goingLeft ? -1 : 1;
TryLinearMove(Time.fixedDeltaTime);
TryLinearMove(dt);
}
else {
if (!isApproaching) {

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

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

Loading…
Cancel
Save