Skip to content

Commit

Permalink
feat(#6, #5): working displaying timer
Browse files Browse the repository at this point in the history
feat(#4): add skip button
feat(#5): hittding upgrades buttons trigger actions (now implement them)
fix: some castle visual problems with upgrade panel
  • Loading branch information
dofranko committed Oct 1, 2021
1 parent db356ed commit a04e588
Show file tree
Hide file tree
Showing 9 changed files with 673 additions and 52 deletions.
512 changes: 487 additions & 25 deletions Assets/Prefabs/Castle.prefab

Large diffs are not rendered by default.

14 changes: 0 additions & 14 deletions Assets/Prefabs/Laser.prefab
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ GameObject:
m_Component:
- component: {fileID: 2334587071612070380}
- component: {fileID: 380814551001927798}
- component: {fileID: -2045613024536224897}
- component: {fileID: 7108923178305197455}
- component: {fileID: 6516146664724804918}
m_Layer: 0
Expand Down Expand Up @@ -51,19 +50,6 @@ MonoBehaviour:
m_Script: {fileID: 11500000, guid: 19807529882e2a7408a7d1aee280d86d, type: 3}
m_Name:
m_EditorClassIdentifier:
--- !u!114 &-2045613024536224897
MonoBehaviour:
m_ObjectHideFlags: 0
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
m_GameObject: {fileID: 380814551001927801}
m_Enabled: 1
m_EditorHideFlags: 0
m_Script: {fileID: 11500000, guid: 07d88badfdda8844ab9cabaef7af3cd9, type: 3}
m_Name:
m_EditorClassIdentifier:
graphicRaycaster: {fileID: 6516146664724804918}
--- !u!223 &7108923178305197455
Canvas:
m_ObjectHideFlags: 0
Expand Down
2 changes: 1 addition & 1 deletion Assets/Scenes/SampleScene.unity
Original file line number Diff line number Diff line change
Expand Up @@ -262,7 +262,7 @@ RectTransform:
m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0}
m_AnchorMin: {x: 0.5, y: 0.5}
m_AnchorMax: {x: 0.5, y: 0.5}
m_AnchoredPosition: {x: 0, y: 0}
m_AnchoredPosition: {x: -1, y: 0.7}
m_SizeDelta: {x: 100, y: 100}
m_Pivot: {x: 0.5, y: 0.5}
--- !u!114 &517536784
Expand Down
15 changes: 13 additions & 2 deletions Assets/Scripts/CastleScript.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,10 @@ public class CastleScript : MonoBehaviour
{
public HealthBar healthBar;
public GameObject upgradesPanel;
public int defense;
public event System.EventHandler OnHideUpgrades;
public event System.EventHandler OnDie;

void Start()
{
healthBar.SetMaxHealth(400);
Expand All @@ -14,23 +18,30 @@ void Start()
public void TakeDamage(int damage)
{
int health = healthBar.GetHealth();
damage -= defense;
if (damage <= 0) damage = 1;
health -= damage;
if (health <= 0) Die();
healthBar.SetHealth(health);
if (health <= 0) Die();
}

void Die()
{
Destroy(gameObject);
OnDie?.Invoke(this, System.EventArgs.Empty);
}

public void DisplayUpgrades()
{
upgradesPanel.SetActive(true);
}

public void HideUpgrades()
public void HideUpgrades(bool invokeHandler = true)
{
upgradesPanel.SetActive(false);
if (invokeHandler)
{
OnHideUpgrades?.Invoke(this, System.EventArgs.Empty);
}
}
}
3 changes: 2 additions & 1 deletion Assets/Scripts/Enemy.cs
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,8 @@ public void Die()
public void TakeDamage(int damage)
{
int health = healthBar.GetHealth();
if (damage - stats.Defense < 1) damage = 1;
damage -= stats.Defense;
if (damage <= 0) damage = 1;
health -= damage;
if (health <= 0) Die();
healthBar.SetHealth(health);
Expand Down
29 changes: 23 additions & 6 deletions Assets/Scripts/EnemySpawner.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,12 @@ enum State
public GameObject enemyPrefab;

private CastleScript castleScript;
private UpgradesSystemScript upgrades;

void Start()
{
var placementScript = GetComponent<PlacementScript>();
placementScript.OnCastleSpawn += OnCastleSpawn;
placementScript.OnCastleSpawn += OnCastleSpawnEventHandler;
}

private void SpawnEnemies()
Expand All @@ -32,7 +33,7 @@ private IEnumerator SpawnEnemiesEnumerator()
yield return new WaitForSeconds(0.0f);
yield return new WaitForSeconds(3.0f);
var castleLocation = GetCastle().transform.position;
for (int i = 0; i < 5; i++)
for (int i = 0; i < 2; i++)
{
Vector3 newLocation = new Vector3(
castleLocation.x - Random.Range(2, 12) * (Random.Range(0, 1) * 2 - 1),
Expand All @@ -41,7 +42,7 @@ private IEnumerator SpawnEnemiesEnumerator()
var enemy = Instantiate(enemyPrefab, newLocation, new Quaternion());
var enemyEnemyComp = enemy.GetComponent<Enemy>();
enemyEnemyComp.CastlePosition = castleLocation;
enemyEnemyComp.OnDie += OnEnemyDie;
enemyEnemyComp.OnDie += OnEnemyDieEventHandler;
}
state = State.Spawned;

Expand All @@ -57,29 +58,31 @@ void Update()
float minutes = Mathf.FloorToInt(timeRemaining / 60);
float seconds = Mathf.FloorToInt(timeRemaining % 60);
var text = string.Format("{0:00}:{1:00}", minutes, seconds);
GetTimerText().SetTimerText(text);
}
else
{
timeRemaining = 0;
state = State.NotSpawned;
GetCastle().HideUpgrades();
GetCastle().HideUpgrades(false);
SpawnEnemies();
}

}
}

private void OnEnemyDie(object? sender, System.EventArgs e)
private void OnEnemyDieEventHandler(object? sender, System.EventArgs e)
{
StartCoroutine(CheckInBetweenWaves());
}

private void OnCastleSpawn(object? sender, System.EventArgs e)
private void OnCastleSpawnEventHandler(object? sender, System.EventArgs e)
{
SpawnEnemies();
}

private IEnumerator CheckInBetweenWaves()

{
yield return new WaitForSeconds(1.0f);
GetCastle();
Expand All @@ -94,13 +97,27 @@ private IEnumerator CheckInBetweenWaves()
}
}
}
private void OnCastleHideUpgradesEventHandler(object? sender, System.EventArgs e)
{
this.timeRemaining = 0;
}

private CastleScript GetCastle()
{
if (!castleScript)
{
castleScript = FindObjectOfType<CastleScript>();
castleScript.OnHideUpgrades += OnCastleHideUpgradesEventHandler;
}
return castleScript;
}

private UpgradesSystemScript GetTimerText()
{
if (!upgrades)
{
upgrades = castleScript.upgradesPanel.GetComponent<UpgradesSystemScript>();
}
return upgrades;
}
}
30 changes: 27 additions & 3 deletions Assets/Scripts/UpgradesSystemScript.cs
Original file line number Diff line number Diff line change
@@ -1,16 +1,20 @@
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

public class UpgradesSystemScript : MonoBehaviour
{

public CastleScript castle;
[SerializeField]
private Text timerText;
private Camera cam;
private int masksToFilter;
void Start()
{
cam = Camera.main;
masksToFilter = LayerMask.GetMask("Raycasted UI");
masksToFilter = LayerMask.GetMask("Raycastable UI");
}

void Update()
Expand All @@ -20,11 +24,31 @@ void Update()
if (Physics.Raycast(cam.transform.position, cam.transform.forward, out RaycastHit hit, 3, masksToFilter))
{
Debug.Log("hit " + hit.transform.name);
if (hit.transform.name == "DefenseImage")
switch (hit.transform.name)
{
FindObjectOfType<CastleScript>().HideUpgrades();
case "DefenseImage":
castle.defense += 1;
Debug.Log("upgraded defense");
break;
case "HealthImage":
break;
// TODO
case "ShieldImage":
break;
// TODO
case "MoneyImage":
break;
// TODO
case "SkipImage":
castle.HideUpgrades();
break;
}
}
}
}

public void SetTimerText(string text)
{
timerText.text = text;
}
}
Binary file added Assets/Textures/SkipImage.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
120 changes: 120 additions & 0 deletions Assets/Textures/SkipImage.png.meta

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

0 comments on commit a04e588

Please sign in to comment.