Boost basics done, need to smooth things

This commit is contained in:
2022-07-06 16:24:47 +05:30
parent 437f295dd7
commit 1cd3453199
121 changed files with 315 additions and 28 deletions

View File

@@ -7,6 +7,8 @@ public class CameraFollower : MonoBehaviour
public bool autoOffset = true;
public Vector3 offset;
public Transform target;
public float minFOV = 11;
public float FOVmultiplier = 5;
public float smoothness = 0.1f;
void Start()
{
@@ -19,6 +21,7 @@ public class CameraFollower : MonoBehaviour
{
if(target==null){return;}
transform.position = Vector3.Lerp(transform.position, target.position + offset, smoothness * Time.deltaTime);
GetComponent<Camera>().orthographicSize = minFOV + ((target.localScale.x - 1) * FOVmultiplier);
}
public void SetTarget(Transform Target){

View File

@@ -1,19 +1,31 @@
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Events;
using UnityEngine.EventSystems;
public class SceneDataHolder : MonoBehaviour
{
public Transform trailCollidersParent;
public GameObject deadScreen;
public EventTrigger boostBtn;
void Awake()
{
SceneData.holder = this;
}
public void OnBoostUp(){
SceneData.OnBoostUp.Invoke();
}
public void OnBoostDown(){
SceneData.OnBoostDown.Invoke();
}
}
public static class SceneData{
public static GameObject localPlayer;
public static SceneDataHolder holder;
public static UnityEvent OnBoostDown = new UnityEvent();
public static UnityEvent OnBoostUp = new UnityEvent();
}

View File

@@ -15,6 +15,10 @@ public class SpaceshipController : NetworkBehaviour
public float trailIncrementRate = 0.5f;
[SyncVar]
public bool dead;
[SyncVar]
public float speed;
[SyncVar(hook=nameof(OnScaleChanged))]
public float scaleMultiplier=1;
public Text pnameTxt;
public Transform body;
public TrailMgr trailMgr;
@@ -22,6 +26,8 @@ public class SpaceshipController : NetworkBehaviour
public float turningSmoothFactor = 0.1f;
public Joystick joystick;
public Vector2 joyInput;
[SyncVar]
public bool boosting;
[Header("Client Prediction")]
public SortedDictionary<int, StatePayload> serverStateBuffer = new SortedDictionary<int, StatePayload>();
@@ -49,14 +55,51 @@ public class SpaceshipController : NetworkBehaviour
trailMgr.trail.time = newValue;
}
void OnBoostDown(){
if(isLocalPlayer){
if(isServer){
boosting=true;
}else{
CmdSetBoosting(true);
}
}
}
void OnBoostUp(){
if(isLocalPlayer){
if(isServer){
boosting=false;
}else{
CmdSetBoosting(false);
}
}
}
void OnScaleChanged(float oldScale, float newScale){
transform.localScale = new Vector3(newScale,newScale,newScale);
trailMgr.trail.startWidth = trailMgr.trail.endWidth = newScale;
if(isLocalPlayer){
SceneData.holder.boostBtn.gameObject.SetActive(newScale>1);
}
}
[Command]
void CmdSetBoosting(bool value){
boosting=value;
}
void Start()
{
scaleMultiplier=1;
if (isLocalPlayer)
{
if (joystick == null) { joystick = FindObjectOfType<Joystick>(); }
FindObjectOfType<CameraFollower>().SetTarget(transform);
string myName = PlayerPrefs.GetString("username");
SceneData.localPlayer = gameObject;
SceneData.OnBoostDown.AddListener(OnBoostDown);
SceneData.OnBoostUp.AddListener(OnBoostUp);
if(isServer){pname=myName;}else{
CmdSetPname(myName);
}
@@ -84,16 +127,33 @@ public class SpaceshipController : NetworkBehaviour
if (isLocalPlayer)
{
joyInput = joystick.input;
//Cheats => TODO: Remove this
if(Input.GetKeyDown(KeyCode.F)){
CmdCheatKills();
}
}
//Simulate on both client and server
if (isLocalPlayer || isServer)
{
body.Translate(new Vector3(0, movingSpeed), body);
body.Translate(new Vector3(0, speed), body);
if (joyInput != Vector2.zero)
{
Turn(joyInput);
}
}
if(isServer){
//boost check
if(boosting && scaleMultiplier > 1){
speed= movingSpeed*2;
scaleMultiplier-=Time.deltaTime;
if(scaleMultiplier<1){scaleMultiplier=1;} //Clamp in case gets lower
}else{
speed=movingSpeed;
}
}
///Diff = rot1 . rot2
///1 = rot1 . rot2 / Diff
///1 * Diff * Diff = rot1.rot2. Diff
@@ -162,7 +222,7 @@ public class SpaceshipController : NetworkBehaviour
Quaternion newRotation = rotation;
for (int i = 0; i < numberOfFrames; i++)
{
newPosition += new Vector3(0, movingSpeed);
newPosition += new Vector3(0, speed);
newRotation = Quaternion.Lerp(newRotation, getTurnAngle(input), turningSmoothFactor * input.magnitude);
}
@@ -251,7 +311,7 @@ public class SpaceshipController : NetworkBehaviour
Quaternion newRotation = rotation;
for (int i = 0; i < numberOfFrames; i++)
{
newPosition += new Vector3(0, movingSpeed);
newPosition += new Vector3(0, speed);
newRotation = Quaternion.Lerp(newRotation, getTurnAngle(input), turningSmoothFactor * input.magnitude);
}
int distanceSinceSent = (int)Vector3.Distance(newPosition, position);
@@ -333,20 +393,34 @@ public class SpaceshipController : NetworkBehaviour
}
SpaceshipController deadPlayer = hit.collider.GetComponent<SpaceshipController>();
if(deadPlayer!=null && !deadPlayer.dead){ // <-- okay we killed someone
if(deadPlayer!=null && !deadPlayer.dead){ // <-- okay we killed someone | KILLCODE
deadPlayer.Die(pname);
Debug.Log($"{pname} killed {deadPlayer.pname}");
Scores+= 10; //TODO: Need to change Scores on kills?
trailTime = trailMgr.trail.time+ trailIncrementRate;
trailMgr.trail.time = trailTime;
OnKill();
}
}
void OnKill(){
Scores+= 10; //TODO: Need to change Scores on kills?
scaleMultiplier+=0.05f;
OnScaleChanged(scaleMultiplier,scaleMultiplier);
trailTime = trailMgr.trail.time+ trailIncrementRate;
trailMgr.trail.time = trailTime;
}
[Command]
void CmdCheatKills(){
OnKill();
}
public void Die(string killer){
Debug.Log($"Sending death signal to {pname} by {killer}");
//Handle Respawning
OnScaleChanged(scaleMultiplier,1);
scaleMultiplier=1;
dead=true;
trailTime = 1;
trailMgr.trail.time = trailTime;