Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Freezables #336

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
103 changes: 103 additions & 0 deletions Assets/KoboldKare/Scripts/Freezable.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using NetStack.Serialization;
using SimpleJSON;
using Photon.Pun;

public class Freezable : MonoBehaviourPun,ISavable, IPunObservable, IPunInstantiateMagicCallback
{ [SerializeField]
private Rigidbody rb;
[SerializeField]
private UsableMachine machineToManage; //Optional disabling of machines when not frozen

private bool frozen=false;
public bool IsFrozen
{
get { return frozen; }
private set { frozen = value; }
}


// Start is called before the first frame update
[PunRPC]
public void FreezeRPC(){
Freeze();
}
public void Freeze(){

rb.constraints = RigidbodyConstraints.FreezePositionX |RigidbodyConstraints.FreezePositionY | RigidbodyConstraints.FreezePositionZ |
RigidbodyConstraints.FreezeRotationX |RigidbodyConstraints.FreezeRotationY |RigidbodyConstraints.FreezeRotationZ;
if(machineToManage!=null)
machineToManage.SetConstructed(true);
frozen=true;
}

[PunRPC]
public void UnfreezeRPC(){
Unfreeze();
}
public void Unfreeze()
{
rb.constraints=RigidbodyConstraints.None;
if(machineToManage!=null)
machineToManage.SetConstructed(false);
frozen=false;

}

public void OnPhotonSerializeView(PhotonStream stream, PhotonMessageInfo info) {
if (stream.IsWriting) {
bool frozenTemp=frozen;
stream.SendNext(frozenTemp);

} else {
bool frozenTemp = (bool)stream.ReceiveNext();
if(frozenTemp!=frozen)
{
if(frozenTemp){
Freeze();
}
else{
Unfreeze();
}
}
}
}

public void OnPhotonInstantiate(PhotonMessageInfo info) {

if (info.photonView.InstantiationData == null) {
return;
}
if (info.photonView.InstantiationData.Length > 0 && info.photonView.InstantiationData[0] is bool) {
bool frozen = (bool)info.photonView.InstantiationData[0];
if(frozen!=IsFrozen)
{
if(frozen){
Freeze();
}
else{
Unfreeze();
}
}
}
if (info.photonView.InstantiationData.Length > 0 && info.photonView.InstantiationData[0] is not BitBuffer) {
throw new UnityException("Unexpected spawn data for container");
}
}
public bool ShouldSave()
{
return true;
}
public void Save(JSONNode node) {
node["frozen"] = IsFrozen;

}

public void Load(JSONNode node) {
bool frozenTemp=node["frozen"];
if(frozenTemp)Freeze();
else Unfreeze();
}
}
62 changes: 62 additions & 0 deletions Assets/KoboldKare/Scripts/MovableFurniture.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using Photon.Pun;
using UnityEngine.Events;

public class MovableFurniture : GenericWeapon, IValuedGood, IGrabbable
{
[SerializeField]
private Transform center;
[SerializeField]
private Freezable freezable;

[PunRPC]
protected override void OnFireRPC(int playerViewID)
{ freezable.Freeze();
}


public float GetWorth()
{
return 15f;
}

public bool CanGrab(Kobold kobold)
{
return !freezable.IsFrozen;
}


[PunRPC]
public void OnGrabRPC(int koboldID)
{
PhotonProfiler.LogReceive(sizeof(int));
}

[PunRPC]
public void OnReleaseRPC(int koboldID, Vector3 velocity)
{
PhotonProfiler.LogReceive(sizeof(int)+sizeof(float)*3);
}

public Transform GrabTransform()
{
return center;
}
// Start is called before the first frame update
void Start()
{


}


// Update is called once per frame
void Update()
{

}


}
10 changes: 10 additions & 0 deletions Assets/KoboldKare/Scripts/PrecisionGrabber.cs
Original file line number Diff line number Diff line change
Expand Up @@ -556,6 +556,11 @@ public void TryGrab() {
[PunRPC]
private void FreezeRPC(int grabViewID, int colliderNum, Vector3 localColliderPosition, Vector3 localHitNormal, Vector3 worldAnchor, Quaternion rotation, bool affRotation) {
PhotonView grabView = PhotonNetwork.GetPhotonView(grabViewID);
if(grabView.GetComponentInChildren<Freezable>()!=null)
{
grabView.RPC("FreezeRPC", RpcTarget.All);
return;
}
Collider[] colliders = grabView.GetComponentsInChildren<Collider>();
frozenGrabs.Add(new Grab(kobold, previewHandAnimator.gameObject, colliders[colliderNum], localColliderPosition, localHitNormal,
worldAnchor, rotation, affRotation, unfreezeSound));
Expand Down Expand Up @@ -629,6 +634,11 @@ public bool TryUnfreeze() {
}

PhotonView otherView = hit.collider.GetComponentInParent<PhotonView>();
if(otherView.GetComponentInChildren<Freezable>()!=null)
{
otherView.RPC("UnfreezeRPC", RpcTarget.All);
return true;
}
if (foundGrabs) {
Rigidbody[] bodies = otherView.GetComponentsInChildren<Rigidbody>();
for (int i = 0; i < bodies.Length; i++) {
Expand Down
108 changes: 108 additions & 0 deletions Assets/KoboldKare/Scripts/Seller.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,108 @@
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Events;
using UnityEngine.UI;
using TMPro;
using Photon.Pun;
using UnityEngine.VFX;
using KoboldKare;

public class Seller : GenericWeapon, IValuedGood, IGrabbable
{
public bool firing = false;
[SerializeField]
private Transform center;
public GameObject scanBeam;
public Transform laserEmitterLocation;
public UnityEvent OnSuccess;
public UnityEvent OnFailure;
public float scanDelay = 0.09f;
private static RaycastHit[] hits = new RaycastHit[32];
private static RaycastHitComparer comparer = new RaycastHitComparer();
[SerializeField] private VisualEffect poof;

[SerializeField]
private GameEventPhotonView soldGameEvent;

private class RaycastHitComparer : IComparer<RaycastHit>
{
public int Compare(RaycastHit x, RaycastHit y)
{
return x.distance.CompareTo(y.distance);
}
}

[PunRPC]
protected override void OnFireRPC(int playerViewID)
{
base.OnFireRPC(playerViewID);
if (firing)
{
return;
}
firing = true;
RaycastHit hit;
Physics.Raycast(laserEmitterLocation.position - laserEmitterLocation.forward * 0.25f, laserEmitterLocation.forward, out hit, 10f,
GameManager.instance.waterSprayHitMask, QueryTriggerInteraction.Ignore);
if(hit.collider.gameObject.GetComponent<Freezable>()!=null )
{
PhotonView view = hit.collider.gameObject.GetComponent<PhotonView>();
soldGameEvent.Raise(view);
poof.SendEvent("TriggerPoof");
float worth;
if(hit.collider.gameObject.GetComponent<InherentWorth>()!=null)
worth=hit.collider.gameObject.GetComponent<InherentWorth>().GetWorth();
else
worth=10f;
PhotonNetwork.Destroy(view.gameObject);
Kobold player =PhotonNetwork.GetPhotonView(playerViewID).GetComponentInParent<Kobold>();
player.GetComponent<MoneyHolder>().AddMoney(worth);
}


}
[PunRPC]
protected override void OnEndFireRPC(int viewID)
{
firing = false;

}
public Vector3 GetWeaponPositionOffset(Transform grabber)
{
return (grabber.up * 0.1f + grabber.right * 0.5f - grabber.forward * 0.25f);
}

public bool ShouldSave()
{
return true;
}
public float GetWorth()
{
return 15f;
}

public bool CanGrab(Kobold kobold)
{
return true;
}

[PunRPC]
public void OnGrabRPC(int koboldID)
{
scanBeam.SetActive(true);
}

[PunRPC]
public void OnReleaseRPC(int koboldID, Vector3 velocity)
{
scanBeam.SetActive(false);
}

public Transform GrabTransform()
{
return center;
}

}