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.
 
 
 

87 lines
1.9 KiB

using System;
using Unity.Netcode;
using UnityEngine;
using UnityEngine.InputSystem;
using UnityEngine.PlayerLoop;
using Random = UnityEngine.Random;
namespace Game {
public class Player : NetworkBehaviour {
private int score;
protected bool GoingLeft, GoingRight;
// Units per second
private float baseSpeed = 10;
// Unit distance from zero
private float baseBorder = 7;
private SpeedModification speedModification;
private BorderModification borderModification;
private float GetSpeed() {
return baseSpeed;
}
private float GetBorder() {
return baseBorder;
}
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 void TryMove(float h) {
Vector2 trans = new Vector2((GoingLeft ? -1 : 0) + (GoingRight ? 1 : 0), 0);
trans *= baseSpeed * h;
transform.Translate(trans);
Debug.Log(trans.magnitude);
if (LeftSide() < -baseBorder)
transform.Translate(Vector2.right * (-baseBorder - LeftSide()));
if (RightSide() > baseBorder)
transform.Translate(Vector2.left * (RightSide() - baseBorder));
}
}
public class AIPlayer : Player {
public enum Difficulty {
VeryEasy, Easy, Medium, Hard, VeryHard
}
private Difficulty difficulty = Difficulty.VeryEasy;
private float GetTargetPosition() {
return 0;
}
private void FixedUpdate() {
float target = GetTargetPosition();
const float h = 1;
GoingLeft = target < X() - h;
GoingRight = target > X() + h;
TryMove(Time.fixedDeltaTime);
}
}
public class RealPlayer : Player {
private void FixedUpdate() {
var keyboard = Keyboard.current;
GoingLeft = keyboard.aKey.isPressed;
GoingRight = keyboard.dKey.isPressed;
TryMove(Time.fixedDeltaTime);
}
}
}