Skip to content

Commit

Permalink
Wavespawner improved
Browse files Browse the repository at this point in the history
  • Loading branch information
BlueIceshard committed Jun 30, 2018
1 parent 4e5b63c commit e0f3127
Show file tree
Hide file tree
Showing 4 changed files with 126 additions and 82 deletions.
2 changes: 1 addition & 1 deletion Neonmania/Assets/New Post-processing Profile.asset
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ MonoBehaviour:
value: 1
intensity:
overrideState: 1
value: 5
value: 3
threshold:
overrideState: 0
value: 1
Expand Down
14 changes: 7 additions & 7 deletions Neonmania/Assets/Scenes/SampleScene.unity
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ RenderSettings:
m_ReflectionIntensity: 1
m_CustomReflection: {fileID: 0}
m_Sun: {fileID: 0}
m_IndirectSpecularColor: {r: 0.1273174, g: 0.13414761, b: 0.12107885, a: 1}
m_IndirectSpecularColor: {r: 0.12731704, g: 0.13414727, b: 0.121078536, a: 1}
m_UseRadianceAmbientProbe: 0
--- !u!157 &3
LightmapSettings:
Expand Down Expand Up @@ -436,6 +436,11 @@ MonoBehaviour:
m_EditorClassIdentifier:
scoreField: {fileID: 2114914618}
waveField: {fileID: 2140120285}
--- !u!4 &1180145008 stripped
Transform:
m_PrefabParentObject: {fileID: 4801776279320918, guid: af3e504195fd1c644885a13c246d151a,
type: 2}
m_PrefabInternal: {fileID: 1234093758}
--- !u!1001 &1234093758
Prefab:
m_ObjectHideFlags: 0
Expand Down Expand Up @@ -478,11 +483,6 @@ Prefab:
m_RemovedComponents: []
m_ParentPrefab: {fileID: 100100000, guid: af3e504195fd1c644885a13c246d151a, type: 2}
m_IsPrefabParent: 0
--- !u!4 &1323457083 stripped
Transform:
m_PrefabParentObject: {fileID: 4953086811605720, guid: af3e504195fd1c644885a13c246d151a,
type: 2}
m_PrefabInternal: {fileID: 1234093758}
--- !u!1001 &1811130874
Prefab:
m_ObjectHideFlags: 0
Expand Down Expand Up @@ -587,7 +587,7 @@ MonoBehaviour:
m_Name:
m_EditorClassIdentifier:
enemy: {fileID: 1683257992604568, guid: da9c70ea3ed54464b955c8fdd666b19d, type: 2}
spawnPlane: {fileID: 1323457083}
spawnPlane: {fileID: 1180145008}
player: {fileID: 1079610139}
gUIController: {fileID: 1148241116}
minionTypes:
Expand Down
149 changes: 77 additions & 72 deletions Neonmania/Assets/Scripts/EnemyController.cs
Original file line number Diff line number Diff line change
@@ -1,79 +1,84 @@
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class EnemyController : MonoBehaviour {

public GameObject player;

public float speed = 1f;
public float checkFrequency = 1f;
public GUIController GUI;

public float timeToDie = 1f;
private float timeDead = 0f;
private bool dead = false;


private float lastCheck = 0f;
private Vector3 direction;

private Rigidbody rb;
private EnemyProperties enemy;

private float lifetime;

// Use this for initialization
void Start () {
rb = GetComponent<Rigidbody>();
enemy = GetComponent<EnemyPropertyController>().properties;
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class EnemyController : MonoBehaviour {

public GameObject player;

public float speed = 1f;
public float checkFrequency = 1f;
public GUIController GUI;

public float timeToDie = 1f;
private float timeDead = 0f;
private bool dead = false;

private float lastCheck = 0f;
private Vector3 direction;

private Rigidbody rb;
private EnemyProperties enemy;

private float lifetime;

// Use this for initialization
void Start () {
rb = GetComponent<Rigidbody>();
enemy = GetComponent<EnemyPropertyController>().properties;
lifetime = 0f;
}

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

if(dead) {

timeDead += Time.deltaTime;

GetComponent<Renderer>().material.SetFloat(Shader.PropertyToID("Vector1_30FACB43"), timeToDie - timeDead);

if(timeToDie - timeDead <= 0) Destroy(this.gameObject);

return;
}

}

internal Action callback;

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

if(dead) {

timeDead += Time.deltaTime;

GetComponent<Renderer>().material.SetFloat(Shader.PropertyToID("Vector1_30FACB43"), timeToDie - timeDead);

if (timeToDie - timeDead <= 0) {
if(callback != null) callback();

Destroy(this.gameObject);
}

return;
}

lastCheck += Time.deltaTime;

lifetime += Time.deltaTime;

GetComponent<Renderer>().material.SetFloat(Shader.PropertyToID("Vector1_30FACB43"), lifetime);

if (lastCheck >= checkFrequency) {
lastCheck = 0;

UpdateEnemyPath();
}

transform.Translate(direction * speed * Time.deltaTime, Space.World);
}

private void OnCollisionEnter(Collision collision) {
if (collision.collider.CompareTag("Player")) {
rb.AddForce(-(player.transform.position - transform.position).normalized * 5, ForceMode.Impulse);
player.GetComponent<PlayerControl>().AddDamage(enemy.attackDamage);
}
}

void UpdateEnemyPath() {
direction = (player.transform.position - transform.position).normalized;
}

public void OnDeath() {
GUI.AddScore(1);
dead = true;
Debug.Log("Dead");
}
}
if (lastCheck >= checkFrequency) {
lastCheck = 0;

UpdateEnemyPath();
}

transform.Translate(direction * speed * Time.deltaTime, Space.World);
}

private void OnCollisionEnter(Collision collision) {
if (collision.collider.CompareTag("Player")) {
rb.AddForce(-(player.transform.position - transform.position).normalized * 5, ForceMode.Impulse);
player.GetComponent<PlayerControl>().AddDamage(enemy.attackDamage);
}
}

void UpdateEnemyPath() {
direction = (player.transform.position - transform.position).normalized;
}

public void OnDeath() {
GUI.AddScore(1);
dead = true;
Debug.Log("Dead");
}
}
43 changes: 41 additions & 2 deletions Neonmania/Assets/Scripts/WaveSpawner.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ public class WaveSpawner : MonoBehaviour {
public EnemyProperties[] minionTypes;
public EnemyProperties[] bossTypes;

private List<GameObject> waveEnemies;

public float timeBetweenWaves = 5f;
public float startCountdown = 2f;
public float timeBetweenEnemies = .5f;
Expand All @@ -22,6 +24,9 @@ public class WaveSpawner : MonoBehaviour {

private Bounds bounds;

private bool isWaveSpawning = false;
private bool bossAlive = false;

// Use this for initialization
void Start () {
Mesh planeMesh = spawnPlane.GetComponent<MeshFilter>().mesh;
Expand All @@ -31,11 +36,12 @@ void Start () {
// Update is called once per frame
void Update() {
if (startCountdown < 0f) {
isWaveSpawning = true;
StartCoroutine(SpawnWave());
startCountdown = timeBetweenWaves;
}

startCountdown -= Time.deltaTime;
if(!isWaveSpawning && !bossAlive) startCountdown -= Time.deltaTime;
}

IEnumerator SpawnWave() {
Expand All @@ -49,6 +55,7 @@ IEnumerator SpawnWave() {
yield return new WaitForSeconds(timeBetweenEnemies);
}

isWaveSpawning = false;
waveNumber++;
}

Expand All @@ -57,7 +64,13 @@ private float SpawnEnemy(float strength, bool boss = false) {
EnemyProperties[] props = boss ? bossTypes : minionTypes;
EnemyProperties[] filteredProps = FilterByStrength(props, strength);

EnemyProperties prop = RandomEnemyProperty(filteredProps);
EnemyProperties prop;

if (boss)
prop = StrongestBoss(filteredProps);
else
prop = RandomEnemyProperty(filteredProps);


if (prop == null) return strength;

Expand All @@ -84,11 +97,36 @@ private float SpawnEnemy(float strength, bool boss = false) {
newEnemy.GetComponent<EnemyPropertyController>().properties = prop;
newEnemy.GetComponent<EnemyController>().GUI = gUIController.GetComponent<GUIController>();

if (boss) {
bossAlive = true;
newEnemy.GetComponent<EnemyController>().callback = BossKilledCallback;
}

//newEnemy.transform.SetParent(transform);

return strength - prop.strengthIndicator;
}

private EnemyProperties StrongestBoss(EnemyProperties[] filteredProps) {
EnemyProperties max = filteredProps[0];

for (int i = 1; i < filteredProps.Length; i++) {
if (max.strengthIndicator < filteredProps[i].strengthIndicator)
max = filteredProps[i];
else if (max.strengthIndicator == filteredProps[i].strengthIndicator) {
if (UnityEngine.Random.value <= 0.5f) max = filteredProps[i];
}
}

return max;
}

private void BossKilledCallback() {
bossAlive = false;

Debug.Log("Yep, that is how you can use callbacks in C#");
}

private EnemyProperties RandomEnemyProperty(EnemyProperties[] filteredProps) {
return filteredProps[(int) (UnityEngine.Random.Range(0, filteredProps.Length))];
}
Expand All @@ -103,4 +141,5 @@ private EnemyProperties[] FilterByStrength(EnemyProperties[] props, float streng

return newProps.ToArray();
}

}

0 comments on commit e0f3127

Please sign in to comment.