Trick tweaks
This commit is contained in:
@@ -233,7 +233,7 @@ MonoBehaviour:
|
|||||||
m_PrefabInstance: {fileID: 0}
|
m_PrefabInstance: {fileID: 0}
|
||||||
m_PrefabAsset: {fileID: 0}
|
m_PrefabAsset: {fileID: 0}
|
||||||
m_GameObject: {fileID: 3937090937419967315}
|
m_GameObject: {fileID: 3937090937419967315}
|
||||||
m_Enabled: 1
|
m_Enabled: 0
|
||||||
m_EditorHideFlags: 0
|
m_EditorHideFlags: 0
|
||||||
m_Script: {fileID: 11500000, guid: b88e05c37f07ccf48bcc436a9d7def5d, type: 3}
|
m_Script: {fileID: 11500000, guid: b88e05c37f07ccf48bcc436a9d7def5d, type: 3}
|
||||||
m_Name:
|
m_Name:
|
||||||
|
|||||||
@@ -192,6 +192,7 @@ MonoBehaviour:
|
|||||||
avoidPlayerWeight: 0.25
|
avoidPlayerWeight: 0.25
|
||||||
avoidTrickWeight: 0.2
|
avoidTrickWeight: 0.2
|
||||||
wanderWeight: 0.4
|
wanderWeight: 0.4
|
||||||
|
maxTricks: 50
|
||||||
jumpImpulse: 6
|
jumpImpulse: 6
|
||||||
jumpCooldown: 1
|
jumpCooldown: 1
|
||||||
jumpChancePerSecond: 0.5
|
jumpChancePerSecond: 0.5
|
||||||
|
|||||||
@@ -1,10 +1,17 @@
|
|||||||
|
using System;
|
||||||
using System.Collections.Generic;
|
using System.Collections.Generic;
|
||||||
using UnityEngine;
|
using UnityEngine;
|
||||||
|
|
||||||
|
using Random = UnityEngine.Random;
|
||||||
|
|
||||||
[RequireComponent(typeof(Rigidbody2D))]
|
[RequireComponent(typeof(Rigidbody2D))]
|
||||||
[RequireComponent(typeof(Collider2D))]
|
[RequireComponent(typeof(Collider2D))]
|
||||||
public class TrickController : MonoBehaviour
|
public class TrickController : MonoBehaviour
|
||||||
{
|
{
|
||||||
|
private static readonly List<TrickController> ActiveTricks = new List<TrickController>();
|
||||||
|
private static PlayerPlatformerController[] cachedPlayers = Array.Empty<PlayerPlatformerController>();
|
||||||
|
private static float nextPlayerCacheTime;
|
||||||
|
|
||||||
[SerializeField] private string treatTag = "Interactable";
|
[SerializeField] private string treatTag = "Interactable";
|
||||||
[SerializeField] private float moveSpeed = 3.5f;
|
[SerializeField] private float moveSpeed = 3.5f;
|
||||||
[SerializeField] private float targetRefreshInterval = 0.4f;
|
[SerializeField] private float targetRefreshInterval = 0.4f;
|
||||||
@@ -18,6 +25,7 @@ public class TrickController : MonoBehaviour
|
|||||||
[SerializeField] private float avoidPlayerWeight = 0.25f;
|
[SerializeField] private float avoidPlayerWeight = 0.25f;
|
||||||
[SerializeField] private float avoidTrickWeight = 0.2f;
|
[SerializeField] private float avoidTrickWeight = 0.2f;
|
||||||
[SerializeField] private float wanderWeight = 0.4f;
|
[SerializeField] private float wanderWeight = 0.4f;
|
||||||
|
[SerializeField] private int maxTricks = 50;
|
||||||
[SerializeField] private float jumpImpulse = 6f;
|
[SerializeField] private float jumpImpulse = 6f;
|
||||||
[SerializeField] private float jumpCooldown = 1f;
|
[SerializeField] private float jumpCooldown = 1f;
|
||||||
[SerializeField] private float jumpChancePerSecond = 0.5f;
|
[SerializeField] private float jumpChancePerSecond = 0.5f;
|
||||||
@@ -44,6 +52,19 @@ public class TrickController : MonoBehaviour
|
|||||||
body.interpolation = RigidbodyInterpolation2D.Interpolate;
|
body.interpolation = RigidbodyInterpolation2D.Interpolate;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private void OnEnable()
|
||||||
|
{
|
||||||
|
if (!ActiveTricks.Contains(this))
|
||||||
|
{
|
||||||
|
ActiveTricks.Add(this);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private void OnDisable()
|
||||||
|
{
|
||||||
|
ActiveTricks.Remove(this);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
private void Update()
|
private void Update()
|
||||||
{
|
{
|
||||||
@@ -88,6 +109,14 @@ public class TrickController : MonoBehaviour
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (!IsGrounded())
|
||||||
|
{
|
||||||
|
var airborne = body.linearVelocity;
|
||||||
|
airborne.x = 0f;
|
||||||
|
body.linearVelocity = airborne;
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
Vector2 desiredVelocity;
|
Vector2 desiredVelocity;
|
||||||
var stopForTreat = false;
|
var stopForTreat = false;
|
||||||
var avoidPlayerDirection = GetAvoidPlayerDirection();
|
var avoidPlayerDirection = GetAvoidPlayerDirection();
|
||||||
@@ -203,6 +232,11 @@ public class TrickController : MonoBehaviour
|
|||||||
|
|
||||||
private void SpawnTrick(Vector3 position)
|
private void SpawnTrick(Vector3 position)
|
||||||
{
|
{
|
||||||
|
if (maxTricks > 0 && ActiveTricks.Count >= maxTricks)
|
||||||
|
{
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
if (trickPrefab != null)
|
if (trickPrefab != null)
|
||||||
{
|
{
|
||||||
Instantiate(trickPrefab, position, Quaternion.identity);
|
Instantiate(trickPrefab, position, Quaternion.identity);
|
||||||
@@ -214,7 +248,13 @@ public class TrickController : MonoBehaviour
|
|||||||
|
|
||||||
private float GetAvoidPlayerDirection()
|
private float GetAvoidPlayerDirection()
|
||||||
{
|
{
|
||||||
var players = FindObjectsByType<PlayerPlatformerController>(FindObjectsSortMode.None);
|
if (Time.time >= nextPlayerCacheTime)
|
||||||
|
{
|
||||||
|
cachedPlayers = FindObjectsByType<PlayerPlatformerController>(FindObjectsSortMode.None);
|
||||||
|
nextPlayerCacheTime = Time.time + 0.5f;
|
||||||
|
}
|
||||||
|
|
||||||
|
var players = cachedPlayers;
|
||||||
if (players == null || players.Length == 0)
|
if (players == null || players.Length == 0)
|
||||||
{
|
{
|
||||||
return 0f;
|
return 0f;
|
||||||
@@ -249,8 +289,7 @@ public class TrickController : MonoBehaviour
|
|||||||
|
|
||||||
private float GetAvoidTrickDirection()
|
private float GetAvoidTrickDirection()
|
||||||
{
|
{
|
||||||
var tricks = FindObjectsByType<TrickController>(FindObjectsSortMode.None);
|
if (ActiveTricks.Count <= 1)
|
||||||
if (tricks == null || tricks.Length <= 1)
|
|
||||||
{
|
{
|
||||||
return 0f;
|
return 0f;
|
||||||
}
|
}
|
||||||
@@ -258,9 +297,9 @@ public class TrickController : MonoBehaviour
|
|||||||
Transform closest = null;
|
Transform closest = null;
|
||||||
var closestDist = float.MaxValue;
|
var closestDist = float.MaxValue;
|
||||||
var origin = transform.position;
|
var origin = transform.position;
|
||||||
for (var i = 0; i < tricks.Length; i++)
|
for (var i = 0; i < ActiveTricks.Count; i++)
|
||||||
{
|
{
|
||||||
var candidate = tricks[i];
|
var candidate = ActiveTricks[i];
|
||||||
if (candidate == null || candidate == this || !candidate.gameObject.activeInHierarchy)
|
if (candidate == null || candidate == this || !candidate.gameObject.activeInHierarchy)
|
||||||
{
|
{
|
||||||
continue;
|
continue;
|
||||||
|
|||||||
Reference in New Issue
Block a user