Hail Fully Networked

This commit is contained in:
2022-04-08 15:37:34 -05:00
parent 26a1265bc9
commit b46dc6101f
13 changed files with 419 additions and 128 deletions

View File

@@ -1,8 +1,10 @@
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using MLAPI;
using MLAPI.Messaging;
public class Hail : MonoBehaviour
public class Hail : NetworkBehaviour
{
private Rigidbody hail;
public GameObject shadow;
@@ -11,42 +13,77 @@ public class Hail : MonoBehaviour
private float m;
private GameObject ShadTemp = null;
ulong shadowID = 0;
void Start() {
if (!IsServer) { return; }
hail = GetComponent<Rigidbody>();//Gets the rigidbody attached to the bolder
ShadTemp = Instantiate(shadow);//Creates the shadow
ShadTemp.transform.position = new Vector3(hail.transform.position.x, hail.transform.position.y - 99.9f, hail.transform.position.z);//Sets it properly directly under the hail and .1 above the ground
ShadTemp.transform.localScale = new Vector3(ShadStSz, 0, ShadStSz);//Scales it to the proper start size
m = hail.transform.localScale.x / 99.9f;//Find the proper rate of growth
SpawnShadowServerRPC();
}
[ServerRpc(RequireOwnership = false)]
private void SpawnShadowServerRPC() {
ShadTemp = Instantiate(shadow, new Vector3(hail.transform.position.x, hail.transform.position.y - 99.9f, hail.transform.position.z), Quaternion.identity);
ShadTemp.transform.localScale = new Vector3(ShadStSz, 0, ShadStSz);//Scales it to the proper start size
ShadTemp.GetComponent<NetworkObject>().Spawn(null, true);
shadowID = ShadTemp.GetComponent<NetworkObject>().NetworkObjectId;
}
void FixedUpdate() {
ScaleShadowServerRPC();//Scales the shadow to that size
DespawnMyselfServerRPC(shadowID);//Sees if it needs to destroy both
}
[ServerRpc(RequireOwnership = false)]
private void ScaleShadowServerRPC() {
float y = -m * (hail.transform.position.y - ShadTemp.transform.position.y) + hail.transform.localScale.x;//Find the proper current size
ShadTemp.transform.localScale = new Vector3(y, 0, y);//Scales the shadow to that size
Destruction();//Sees if it needs to destroy both
ScaleShadowClientRPC(shadowID, y);
}
[ClientRpc]
private void ScaleShadowClientRPC(ulong sID, float size) {
GameObject[] shadows = GameObject.FindGameObjectsWithTag("Shadows");
foreach (GameObject shad in shadows) {
if (shad.GetComponent<NetworkObject>().NetworkObjectId == sID) {
shad.transform.localScale = new Vector3(size, 0, size);
}
}
}
void OnTriggerEnter(Collider other) {
if (other.tag == "Player") {
if (other.tag == "ArcherTarget") {
GameObject player = other.gameObject;//Turns the collider into a game object
float Power = hail.velocity.magnitude * 10;//Finds the power of the hail by using it's velocity and a scaler
float Power = 50;//Finds the power of the hail by using it's velocity and a scaler
//Find the dirrection of launch by finding the direction between the two points of the player and the Hail
float px = player.transform.position.x;
float pz = player.transform.position.z;
float hx = hail.transform.position.x;
float hz = hail.transform.position.z;
float hx = gameObject.transform.position.x;
float hz = gameObject.transform.position.z;
//Direction pointing away from the center of the Hail
Vector3 Dir = new Vector3(px - hx, 0, pz - hz);
player.GetComponent<MoveStateManager>().GetHit(Dir, Power); //Ragdolls the player in the direction of the bolder depending on it's speed
other.GetComponent<MoveStateManager>().GetHit(Dir, Power);
}
}
//Destroys the Hail and its shadow
void Destruction() {
if(hail.transform.position.y <= ShadTemp.transform.position.y) {
Destroy(ShadTemp);
Destroy(gameObject);
[ServerRpc(RequireOwnership = false)]
private void DespawnMyselfServerRPC(ulong sID) {
GameObject[] shadows = GameObject.FindGameObjectsWithTag("Shadows");
foreach (GameObject shad in shadows) {
if (shad.GetComponent<NetworkObject>().NetworkObjectId == sID) {
if (hail.transform.position.y <= shad.transform.position.y) {
shad.GetComponent<NetworkObject>().Despawn(true);
this.gameObject.GetComponent<NetworkObject>().Despawn(true);
}
}
}
}
}
}

View File

@@ -1,8 +1,10 @@
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using MLAPI;
using MLAPI.Messaging;
public class HailArea : MonoBehaviour
public class HailArea : NetworkBehaviour
{
// Update is called once per frame
private float Xmax = 15f;
@@ -16,6 +18,8 @@ public class HailArea : MonoBehaviour
public GameObject Hail;
private GameObject spawnedHail;
int timer = 0;
int timeMax = 75;
@@ -39,24 +43,33 @@ public class HailArea : MonoBehaviour
}
position = new Vector3(position.x, 100+height, position.z); //find ground and set y occordingly
SpawnHail(diameter, position);
SpawnHailServerRPC(diameter, position);
}
timer++;
Lifetime++;
if (Lifetime == 3000) {
Destruction();
DespawnMyselfServerRPC();
}
}
// Spawns a singular Hail piece
void SpawnHail(float size, Vector3 pos)
{
//Spawn HailBall and its shadow
GameObject HailTemp = null;
HailTemp = Instantiate(Hail);
HailTemp.transform.position = pos;
HailTemp.transform.localScale = new Vector3(size, size, size);
[ServerRpc(RequireOwnership = false)]
void SpawnHailServerRPC(float size, Vector3 pos) {
spawnedHail = Instantiate(Hail, pos, Quaternion.identity);
spawnedHail.GetComponent<NetworkObject>().Spawn(null, true);
ResizeHailClientRPC(spawnedHail.GetComponent<NetworkObject>().NetworkObjectId, size);
}
[ClientRpc]
private void ResizeHailClientRPC(ulong hailID, float size) {
GameObject[] hails = GameObject.FindGameObjectsWithTag("Hail");
foreach (GameObject hail in hails) {
if (hail.GetComponent<NetworkObject>().NetworkObjectId == hailID) {
hail.transform.localScale = new Vector3(size, size, size);
}
}
}
public void SetArea(float LeftBound, float RightBound, float TopBound, float BottomBound) {
@@ -87,7 +100,8 @@ public class HailArea : MonoBehaviour
}
}
void Destruction() {
Destroy(gameObject);
[ServerRpc(RequireOwnership = false)]
private void DespawnMyselfServerRPC() {
this.gameObject.GetComponent<NetworkObject>().Despawn(true);
}
}

View File

@@ -332,10 +332,10 @@ public class KingPlace : NetworkBehaviour
GridCheck(RowNumb, Box, ref HailPlacing);//Makes sure the Box is a valid position
}
//Instanciate HailArea based on PlaceTemp = Top Left and HailCorner = Bottom Right
if (HailPlacing == false) {
GameObject Temp;
Temp = Instantiate(HailObject);
Temp.GetComponent<HailArea>().SetArea(PlaceTemp.transform.position.x, HailCorner.transform.position.x, PlaceTemp.transform.position.z, HailCorner.transform.position.z);
if (HailPlacing == false) {
Vector4 spawnLoc = new Vector4(PlaceTemp.transform.position.x, HailCorner.transform.position.x, PlaceTemp.transform.position.z, HailCorner.transform.position.z);
SpawnHailServerRPC(spawnLoc);
Destroy(PlaceTemp);
Destroy(HailCorner);
}
@@ -414,4 +414,13 @@ public class KingPlace : NetworkBehaviour
placedObj.GetComponent<NetworkObject>().Spawn(null, true);
}
[ServerRpc(RequireOwnership = false)]
private void SpawnHailServerRPC(Vector4 spawnLoc) {
placedObj = Instantiate(HailObject, Vector3.zero, Quaternion.identity);
placedObj.GetComponent<HailArea>().SetArea(spawnLoc.x, spawnLoc.y, spawnLoc.z, spawnLoc.w);
placedObj.GetComponent<NetworkObject>().Spawn(null, true);
}
}