Handle HardObstacle solving by player

This commit is contained in:
Antoine H 2021-01-28 15:34:58 +01:00
parent 377916ffb2
commit 3bc7c9c7ee
6 changed files with 470 additions and 11 deletions

View file

@ -7,12 +7,26 @@ using UnityEngine.AI;
[RequireComponent(typeof(Animator))]
[RequireComponent(typeof(Collider2D))]
[RequireComponent(typeof(NavMeshObstacle))]
public class HardObstacle : MonoBehaviour
public class HardObstacle : MonoBehaviour, IUsable
{
[SerializeField]
float lifeTime= -1.0f, waitTime= 30.0f; //Time active/waiting before self-destruct (Negative value to prevent self-destruct)
float lifeTimer;
[SerializeField]
UITimer UIStopTimer = null; //Script of the UI display
[SerializeField]
float stopTime=1.0f; //Time to stop fight by tavernkeeper
float stopTimer=0.0f;
bool playerInteracting = false; //Wether the player is interacting w/ the workshop
float interactionCd = 0.0f; //Time to consider the interaction stopped
[SerializeField]
float interactionSmoothing = 0.0f; //% of action_cd added to the interaction CD for smooth continued interaction
protected float cdTimer = 0.0f;
SpriteRenderer user_renderer = null; //Sprite renderer of the user (to turn insvisible)
List<Client_controller> angryClients = new List<Client_controller>(); //Clients in the fight
Collider2D ObsCollider;
@ -20,11 +34,53 @@ public class HardObstacle : MonoBehaviour
// SpriteRenderer ObsRenderer;
Animator animator;
bool gameRunning = true;
bool gameRunning = true; //Wehter the game is running (for clean-up purpose)
//Handle objects interactions w/ obstacle
//Return wether the object is taken from tavernkeeper
public bool use(GameObject userObject)
{
//Handle continuous interaction w/ tavernkeeper
if(userObject.tag=="Player")
{
if(stopTimer<stopTime) //Continue to stop fight
{
Debug.Log(gameObject.name+" still used by "+userObject.name);
if(interactionCd<0.01f) //No interaction CD ?
{
user_renderer = userObject.GetComponent<SpriteRenderer>(); //Renderer for visual effects
//Reset interaction CD
Tavernkeeper_controller player = userObject.GetComponent<Tavernkeeper_controller>();
if(player != null)
interactionCd=player.action_cd*(1.0f+interactionSmoothing); //=action_cd+ interactionSmoothing(%) action_cd (for smooth continued interaction)
else
Debug.LogWarning(userObject.name+" cannot have a continuous interaction on "+gameObject.name);
}
playerInteracting = true; //Set interaction indicator
cdTimer = interactionCd; //Reset Interaction CD
//Visual effect (user disappear)
if(user_renderer != null)
user_renderer.color=Color.clear;
}
else //Fight stopped
Destroy(gameObject);
}
return false; //No object taken
}
// Start is called before the first frame update
void Start()
{
if(gameObject.layer != LayerMask.NameToLayer("Interactions"))
Debug.LogWarning(gameObject.name+" layer should be set to 'Interactions' to work properly");
if(gameObject.tag != "Usable")
Debug.LogWarning(gameObject.name+" tag should be set to 'Usable' to work properly");
if(UIStopTimer is null)
Debug.LogWarning(gameObject.name+" doesn't have a UIStopTimer set");
lifeTimer=waitTime; //Start by waiting client
ObsCollider = GetComponent<Collider2D>();
@ -39,10 +95,34 @@ public class HardObstacle : MonoBehaviour
// Update is called once per frame
void Update()
{
//Life time update
lifeTimer -= Time.deltaTime;
if(lifeTimer<0)
Destroy(gameObject);
//Player interactions update
if(user_renderer != null && !playerInteracting)
user_renderer.color=Color.white; //User reappear if there's one
if(playerInteracting)
{
//Continue stopping fight
if(stopTimer<stopTime)
stopTimer+=Time.deltaTime;
//Update interaction CD
cdTimer-=Time.deltaTime;
if (cdTimer<0)
playerInteracting=false; //Reset interaction indicator
}
else if(stopTimer>0) //Decrease fight stopping jauge if not interacting
stopTimer-=Time.deltaTime*0.5f;
//UI update
if(UIStopTimer != null)
{
UIStopTimer.SetValue(stopTimer/stopTime);
UIStopTimer.gameObject.SetActive(stopTimer>0); //Set active if timer>0
}
}
void OnTriggerEnter2D(Collider2D other)
@ -76,11 +156,16 @@ public class HardObstacle : MonoBehaviour
void OnDestroy()
{
if(gameRunning) //Only apply if game is still running
{
foreach(Client_controller client in angryClients) //Clients return to their previous behavior
{
client.gameObject.SetActive(true);
client.assignToEvent(); //Restore previous behavior
}
if(user_renderer != null) //User reappear if there's one
user_renderer.color=Color.white;
}
EventManager.Instance.removeEvent(gameObject);
}