Skip to content

Commit

Permalink
Tree boss spit attack
Browse files Browse the repository at this point in the history
  • Loading branch information
someone42 committed Feb 5, 2023
1 parent 38be7af commit d94a468
Show file tree
Hide file tree
Showing 7 changed files with 159 additions and 17 deletions.
2 changes: 2 additions & 0 deletions Assets/Prefabs/Potato.prefab
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,8 @@ MonoBehaviour:
acceleration: 5
maximumSpeed: 5
startRight: 1
flyIgnoreCollisions: 0.5
isFlying: 0
BossScObj: {fileID: 0}
--- !u!114 &4134442384279228947
MonoBehaviour:
Expand Down
23 changes: 23 additions & 0 deletions Assets/Prefabs/TreeBoss.prefab
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ GameObject:
- component: {fileID: 23277807649979615}
- component: {fileID: 4856363347904904803}
- component: {fileID: 7708893993313149470}
- component: {fileID: 6708470962637682533}
- component: {fileID: 3770758557608109406}
m_Layer: 0
m_Name: TreeBoss
Expand Down Expand Up @@ -103,6 +104,7 @@ MonoBehaviour:
chanceOfSpit: 0.1
chanceOfMelee: 0.1
spikeLockout: 8
spitLockout: 5
BossScObj: {fileID: 0}
--- !u!114 &4856363347904904803
MonoBehaviour:
Expand Down Expand Up @@ -144,6 +146,27 @@ MonoBehaviour:
initialDistanceFactor: 3
rootSpikePrefab: {fileID: 1396250162364260641, guid: 9a61359e32e7c374a93d60af9344f8fd,
type: 3}
--- !u!114 &6708470962637682533
MonoBehaviour:
m_ObjectHideFlags: 0
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
m_GameObject: {fileID: 23277807649979601}
m_Enabled: 1
m_EditorHideFlags: 0
m_Script: {fileID: 11500000, guid: 5cb11b1b23e0f194088cd2889a798245, type: 3}
m_Name:
m_EditorClassIdentifier:
numSpawns: 4
initialDelay: 3
spawnTimeInterval: 0.2
potatoSpeed: 20
minAngle: 20
maxAngle: 40
spawnDisplacement: {x: 0, y: 9, z: 0}
potatoPrefab: {fileID: 2702485830317621532, guid: 33a020d0522535c4fb3623ed886cc51e,
type: 3}
--- !u!65 &3770758557608109406
BoxCollider:
m_ObjectHideFlags: 0
Expand Down
6 changes: 3 additions & 3 deletions Assets/Scenes/BossTestScene.unity
Original file line number Diff line number Diff line change
Expand Up @@ -777,17 +777,17 @@ PrefabInstance:
- target: {fileID: 23277807649979615, guid: 8d1a5c0cb9ffa9c4fb6011fd611520e5,
type: 3}
propertyPath: chanceOfLaugh
value: 0.9
value: 0.1
objectReference: {fileID: 0}
- target: {fileID: 23277807649979615, guid: 8d1a5c0cb9ffa9c4fb6011fd611520e5,
type: 3}
propertyPath: chanceOfSpike
value: 0
value: 0.1
objectReference: {fileID: 0}
- target: {fileID: 23277807649979615, guid: 8d1a5c0cb9ffa9c4fb6011fd611520e5,
type: 3}
propertyPath: chanceOfSpecialIdle
value: 0.1
value: 0.15
objectReference: {fileID: 0}
- target: {fileID: 1107897357665298954, guid: 8d1a5c0cb9ffa9c4fb6011fd611520e5,
type: 3}
Expand Down
57 changes: 57 additions & 0 deletions Assets/Scripts/PotatoSpammer.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

// Spawns and throws a bunch of potatoes
public class PotatoSpammer : MonoBehaviour
{
[Tooltip("Number of potatoes to spawn")]
public int numSpawns = 12;
[Tooltip("Initial delay before the first spawn")]
public float initialDelay = 1.5f;
[Tooltip("Time interval in seconds between spawns")]
public float spawnTimeInterval = 0.1f;
[Tooltip("Speed of potatoes being launched")]
public float potatoSpeed = 10.0f;
[Tooltip("Potato launch angle minimum")]
public float minAngle = 35.0f;
[Tooltip("Potato launch angle maximum")]
public float maxAngle = 55.0f;
[Tooltip("Displacement from parent position that the potatoes will spawn from")]
public Vector3 spawnDisplacement;

[Tooltip("Put potato prefab object here")]
public GameObject potatoPrefab;

public void Spawn()
{
StartCoroutine("SpawnPotatoes");
}

IEnumerator SpawnPotatoes()
{
yield return new WaitForSeconds(initialDelay);
for (int i = 0; i < numSpawns; i++)
{
GameObject g = Instantiate(potatoPrefab, transform);
g.transform.rotation = Quaternion.Euler(new Vector3(0.0f, 0.0f, Random.Range(0.0f, 360.0f)));
g.transform.position = transform.position + spawnDisplacement;
Debug.DrawLine(transform.position, g.transform.position);
Rigidbody rb = g.GetComponent<Rigidbody>();
float angle = Random.Range(minAngle, maxAngle);
rb.velocity = new Vector3(-potatoSpeed * Mathf.Cos(angle * Mathf.Deg2Rad), potatoSpeed * Mathf.Sin(angle * Mathf.Deg2Rad), 0.0f);
g.GetComponent<RollerController>().Fly();
yield return new WaitForSeconds(spawnTimeInterval);
}
}

// Start is called before the first frame update
void Start()
{
}

// Update is called once per frame
void Update()
{
}
}
11 changes: 11 additions & 0 deletions Assets/Scripts/PotatoSpammer.cs.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

65 changes: 52 additions & 13 deletions Assets/Scripts/RollerController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,16 @@ public class RollerController : Enemy
public float maximumSpeed = 2.0f;
[Tooltip("Whether the roller starts going right")]
public bool startRight = true;
[Tooltip("How long to ignore collisions when starting to fly")]
public float flyIgnoreCollisions = 0.2f;
public bool isFlying = false;

private bool isPositive;
private float currentVelocity;
private Rigidbody rb;
private LookAhead lookahead;
private Collider collider;
private float whenStartedFlying;

[Header("Leave Blank if not a boss enemy")]
[SerializeField] private Bosses_ScriptableObj BossScObj;
Expand All @@ -22,6 +27,10 @@ void Start()
{
rb = GetComponent<Rigidbody>();
lookahead = GetComponent<LookAhead>();
if (collider == null)
{
collider = GetComponent<Collider>();
}
// Half go right, half go left
isPositive = startRight;

Expand All @@ -31,33 +40,63 @@ void Start()
}
}

// Have the roller fly freely
public void Fly()
{
isFlying = true;
whenStartedFlying = Time.fixedTime;
if (collider == null)
{
collider = GetComponent<Collider>();
}
collider.enabled = false;
}

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

void FixedUpdate()
{
// Accelerate up to maximum speed in the chosen direction
float velocityChange = acceleration * Time.fixedDeltaTime;
if (Mathf.Abs(currentVelocity) < maximumSpeed)
if (!isFlying)
{
if (isPositive)
// Accelerate up to maximum speed in the chosen direction
float velocityChange = acceleration * Time.fixedDeltaTime;
if (Mathf.Abs(currentVelocity) < maximumSpeed)
{
currentVelocity += velocityChange;
if (isPositive)
{
currentVelocity += velocityChange;
}
else
{
currentVelocity -= velocityChange;
}
}
else
rb.velocity = new Vector3(currentVelocity, rb.velocity.y, rb.velocity.z);
rb.angularVelocity = new Vector3(0.0f, 0.0f, -currentVelocity);
if (!lookahead.CheckIfCanMoveInFront(transform, isPositive))
{
currentVelocity -= velocityChange;
// Change direction
currentVelocity = 0.0f;
isPositive = !isPositive;
}
}
rb.velocity = new Vector3(currentVelocity, rb.velocity.y, rb.velocity.z);
rb.angularVelocity = new Vector3(0.0f, 0.0f, -currentVelocity);
if (!lookahead.CheckIfCanMoveInFront(transform, isPositive))
else
{
if ((Time.fixedTime - whenStartedFlying) >= flyIgnoreCollisions)
{
collider.enabled = true;
}
}
}

private void OnCollisionEnter(Collision collision)
{
if (isFlying)
{
// Change direction
currentVelocity = 0.0f;
isPositive = !isPositive;
isFlying = false;
}
}
}
12 changes: 11 additions & 1 deletion Assets/Scripts/TreeBossController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -29,13 +29,17 @@ enum States
public float chanceOfMelee = 0.05f;
[Tooltip("Seconds to lock out spike or root attacks after a spike or root attack")]
public float spikeLockout = 5.0f;
[Tooltip("Seconds to lock out spit attacks after a spit attack")]
public float spitLockout = 5.0f;

private Animator animator;
private RootWaveSpawn rootWave;
private RootSpikeSpawn rootSpike;
private PotatoSpammer potatoSpammer;

private States currentState;
private float lastSpike;
private float lastSpit;

[Header("Leave Blank if not a boss enemy")]
[SerializeField] private Bosses_ScriptableObj BossScObj;
Expand All @@ -47,6 +51,7 @@ void Start()
animator = GetComponentInChildren<Animator>();
rootWave = GetComponent<RootWaveSpawn>();
rootSpike = GetComponent<RootSpikeSpawn>();
potatoSpammer = GetComponent<PotatoSpammer>();
animator.SetTrigger("StartLaugh");
// Because the animator is in a child object, we can't get it to call a function here,
// instead, use AnimationEventsHandler to forward the calls to our AnimationClipEnded method
Expand Down Expand Up @@ -84,7 +89,6 @@ private bool chooseNewState()
{
return false; // lockout
}
Debug.Log("StartLaugh");
animator.SetTrigger("StartLaugh");
rootSpike.Spawn();
lastSpike = Time.fixedTime;
Expand All @@ -107,7 +111,13 @@ private bool chooseNewState()
randomVar -= chanceOfSpike;
if (randomVar < chanceOfSpit)
{
if ((Time.fixedTime - lastSpit) < spitLockout)
{
return false; // lockout
}
animator.SetTrigger("StartSpit");
potatoSpammer.Spawn();
lastSpit = Time.fixedTime;
}
else
{
Expand Down

0 comments on commit d94a468

Please sign in to comment.