From 09c0b2eed74a77010ba686061b147c9cace88ed6 Mon Sep 17 00:00:00 2001 From: Jakob Stendahl Date: Sat, 7 Oct 2017 22:57:48 +0200 Subject: Trur æ la te VCS, ikke meninga, men æ trur itj de e nå problem egt MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- td/Assets/Scripts/Enemy.cs | 31 +++++---- td/Assets/Scripts/EnemySpawner.cs | 37 ++++++----- td/Assets/Scripts/Projectile.cs | 42 ++++++++++++ td/Assets/Scripts/Projectile.cs.meta | 12 ++++ td/Assets/Scripts/cameraHandler.cs | 66 +++++++++---------- td/Assets/Scripts/castle.cs | 23 +++++++ td/Assets/Scripts/castle.cs.meta | 12 ++++ td/Assets/Scripts/developerMode.cs | 56 ++++++++-------- td/Assets/Scripts/enableChild.cs | 2 +- td/Assets/Scripts/gameStats.cs | 56 ++++++++-------- td/Assets/Scripts/mainGUI.cs | 122 +++++++++++++++++------------------ td/Assets/Scripts/player.cs | 53 +++++++-------- td/Assets/Scripts/tower.cs | 112 ++++++++++++++++---------------- td/Assets/Scripts/waveSpawner.cs | 44 ++++++------- 14 files changed, 376 insertions(+), 292 deletions(-) create mode 100644 td/Assets/Scripts/Projectile.cs create mode 100644 td/Assets/Scripts/Projectile.cs.meta create mode 100644 td/Assets/Scripts/castle.cs create mode 100644 td/Assets/Scripts/castle.cs.meta (limited to 'td/Assets/Scripts') diff --git a/td/Assets/Scripts/Enemy.cs b/td/Assets/Scripts/Enemy.cs index 4542902..3074003 100644 --- a/td/Assets/Scripts/Enemy.cs +++ b/td/Assets/Scripts/Enemy.cs @@ -1,5 +1,4 @@ -using System.Collections; -using System.Collections.Generic; +using System.Collections.Generic; using UnityEngine; public class Enemy : MonoBehaviour { @@ -7,28 +6,28 @@ public class Enemy : MonoBehaviour { * Currently it follows the pathway, and dies when reacing the end */ [Header("Attributes")] - public float speed; // Speed multiplier - public int initialHp; // HealthPoints - public int damage; - public List waypoints; // Pathway waypoints, should be set by the spawner + public float Speed; // Speed multiplier + public int InitialHp; // HealthPoints + public int Damage; + public List Waypoints; // Pathway waypoints, should be set by the spawner [Header("Scripting vars")] - public player player; // Reference to the player object, should be set when instantiating + public Player Player; // Reference to the player object, should be set when instantiating - Vector3 waypointPos; // Current waypoint position - int waypointNum = -1; // Using minus one so that first addition returns 0, first element in array + private Vector3 _waypointPos; // Current waypoint position + private int _waypointNum = -1; // Using minus one so that first addition returns 0, first element in array void Update () { - if ( (transform.position == waypointPos && waypointNum + 1 < waypoints.Count) || waypointNum == -1) { - waypointNum++; - waypointPos = new Vector3 (waypoints [waypointNum].x, 0.483f, waypoints [waypointNum].z); + if ( (transform.position == _waypointPos && _waypointNum + 1 < Waypoints.Count) || _waypointNum == -1) { + _waypointNum++; + _waypointPos = new Vector3 (Waypoints [_waypointNum].x, 0.483f, Waypoints [_waypointNum].z); } - float transformStep = speed * Time.deltaTime; - transform.position = Vector3.MoveTowards (transform.position, waypointPos, transformStep); + float transformStep = Speed * Time.deltaTime; + transform.position = Vector3.MoveTowards (transform.position, _waypointPos, transformStep); // Selfdestruct if object reached the end - if (waypointNum + 1 >= waypoints.Count) { - player.decreaseHealth (damage); + if (_waypointNum + 1 >= Waypoints.Count) { + Player.DecreaseHealth (Damage); Destroy (gameObject); return; } diff --git a/td/Assets/Scripts/EnemySpawner.cs b/td/Assets/Scripts/EnemySpawner.cs index ee0d3d6..7ee8680 100644 --- a/td/Assets/Scripts/EnemySpawner.cs +++ b/td/Assets/Scripts/EnemySpawner.cs @@ -6,42 +6,41 @@ public class EnemySpawner : MonoBehaviour { /* This is a class that spawns an enemy with a random interval * it is not very good, but is what it needs to be for testing purposes */ // TODO Add wave system with increasing difficulty - - public Enemy enemyPrefab; - public Transform pathWay; + public Enemy EnemyPrefab; + public Transform PathWay; [Header("Scripting vars")] - public player player; // Reference to the player object, should be set when instantiating + public Player Player; // Reference to the player object, should be set when instantiating - private Transform parentObject; + private Transform _parentObject; - List waypoints = new List(); - int next = 1; - int n = 0; + List _waypoints = new List(); + int _next = 1; + int _n = 0; void Awake() { - foreach (Transform child in pathWay) { - waypoints.Add (child.position); + foreach (Transform child in PathWay) { + _waypoints.Add (child.position); } } void Start() { - parentObject = transform.Find ("enemies").gameObject.GetComponent (); + _parentObject = transform.Find ("enemies").gameObject.GetComponent (); } void Update () { - n++; + _n++; - if (n == next) { - n = 0; - next = (int)Random.Range (50, 400); + if (_n == _next) { + _n = 0; + _next = (int)Random.Range (50, 400); - Enemy newEnemy = Instantiate (enemyPrefab, new Vector3(0, 0, 0), Quaternion.identity, parentObject); + Enemy newEnemy = Instantiate (EnemyPrefab, new Vector3(0, 0, 0), Quaternion.identity, _parentObject); Enemy script = newEnemy.GetComponent (); Transform transform = newEnemy.GetComponent (); - script.waypoints = waypoints; - script.speed = Random.Range (0.3f, 1.2f); - script.player = player; + script.Waypoints = _waypoints; + script.Speed = Random.Range (0.3f, 1.2f); + script.Player = Player; transform.position = new Vector3 (0.93f, 0.483f, 0f); } diff --git a/td/Assets/Scripts/Projectile.cs b/td/Assets/Scripts/Projectile.cs new file mode 100644 index 0000000..0cdef16 --- /dev/null +++ b/td/Assets/Scripts/Projectile.cs @@ -0,0 +1,42 @@ +using UnityEngine; + +public class Projectile : MonoBehaviour { + + public float Speed = 70f; + public int PointsPerHit; + [Header("Scripting vars")] + public Player Player; // Reference to the player object, should be set when instantiating + private Transform _target; + + public void Seek(Transform target) { + _target = target; + } + + + void Update () { + + if (_target == null) { + Destroy (gameObject); + return; + } + + Vector3 direction = _target.position - transform.position; + float distanceThisFrame = Speed * Time.deltaTime; + + if (direction.magnitude <= distanceThisFrame) { + HitTarget (); + return; + } + + transform.Translate (direction.normalized * distanceThisFrame, Space.World); + + + } + + void HitTarget() { + Player.ScoreAdd (PointsPerHit); + Destroy (_target.gameObject); + Destroy (gameObject); + } + +} diff --git a/td/Assets/Scripts/Projectile.cs.meta b/td/Assets/Scripts/Projectile.cs.meta new file mode 100644 index 0000000..a95b993 --- /dev/null +++ b/td/Assets/Scripts/Projectile.cs.meta @@ -0,0 +1,12 @@ +fileFormatVersion: 2 +guid: 4a1928566183240ea8566b659c4b3d5f +timeCreated: 1507300755 +licenseType: Free +MonoImporter: + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: diff --git a/td/Assets/Scripts/cameraHandler.cs b/td/Assets/Scripts/cameraHandler.cs index c1c4cae..1655509 100644 --- a/td/Assets/Scripts/cameraHandler.cs +++ b/td/Assets/Scripts/cameraHandler.cs @@ -1,8 +1,6 @@ -using System.Collections; -using System.Collections.Generic; -using UnityEngine; +using UnityEngine; -public class cameraHandler : MonoBehaviour { +public class CameraHandler : MonoBehaviour { // TODO Fiks panning, ser idiotisk ut nå så jeg har satt panSpeed til 0. (I editoren) public float PanSpeed = 20f; @@ -14,17 +12,17 @@ public class cameraHandler : MonoBehaviour { public static readonly float[] BoundsZ = new float[]{-8f, 8f}; public static readonly float[] ZoomBounds = new float[]{1f, 5f}; - private Camera cam; + private Camera _cam; - private bool panActive; - private Vector3 lastPanPosition; - private int panFingerId; // Touch mode only + private bool _panActive; + private Vector3 _lastPanPosition; + private int _panFingerId; // Touch mode only - private bool zoomActive; - private Vector2[] lastZoomPositions; // Touch mode only + private bool _zoomActive; + private Vector2[] _lastZoomPositions; // Touch mode only void Awake() { - cam = GetComponent(); + _cam = GetComponent(); } void Update() { @@ -46,43 +44,43 @@ public class cameraHandler : MonoBehaviour { switch(Input.touchCount) { case 1: // Panning - zoomActive = false; + _zoomActive = false; // If the touch began, capture its position and its finger ID. // Otherwise, if the finger ID of the touch doesn't match, skip it. Touch touch = Input.GetTouch(0); if (touch.phase == TouchPhase.Began) { - lastPanPosition = touch.position; - panFingerId = touch.fingerId; - panActive = true; - } else if (touch.fingerId == panFingerId && touch.phase == TouchPhase.Moved) { + _lastPanPosition = touch.position; + _panFingerId = touch.fingerId; + _panActive = true; + } else if (touch.fingerId == _panFingerId && touch.phase == TouchPhase.Moved) { PanCamera(touch.position); } break; case 2: // Zooming - panActive = false; + _panActive = false; Vector2[] newPositions = new Vector2[]{Input.GetTouch(0).position, Input.GetTouch(1).position}; - if (!zoomActive) { - lastZoomPositions = newPositions; - zoomActive = true; + if (!_zoomActive) { + _lastZoomPositions = newPositions; + _zoomActive = true; } else { // Zoom based on the distance between the new positions compared to the // distance between the previous positions. float newDistance = Vector2.Distance(newPositions[0], newPositions[1]); - float oldDistance = Vector2.Distance(lastZoomPositions[0], lastZoomPositions[1]); + float oldDistance = Vector2.Distance(_lastZoomPositions[0], _lastZoomPositions[1]); float offset = newDistance - oldDistance; ZoomCamera(offset, ZoomSpeedTouch); - lastZoomPositions = newPositions; + _lastZoomPositions = newPositions; } break; default: - panActive = false; - zoomActive = false; + _panActive = false; + _zoomActive = false; break; } } @@ -92,41 +90,41 @@ public class cameraHandler : MonoBehaviour { // On mouse up, disable panning. // If there is no mouse being pressed, do nothing. if (Input.GetMouseButtonDown(0)) { - panActive = true; - lastPanPosition = Input.mousePosition; + _panActive = true; + _lastPanPosition = Input.mousePosition; } else if (Input.GetMouseButtonUp(0)) { - panActive = false; + _panActive = false; } else if (Input.GetMouseButton(0)) { PanCamera(Input.mousePosition); } // Check for scrolling to zoom the camera float scroll = Input.GetAxis("Mouse ScrollWheel"); - zoomActive = true; + _zoomActive = true; ZoomCamera(scroll, ZoomSpeedMouse); - zoomActive = false; + _zoomActive = false; } void ZoomCamera(float offset, float speed) { - if (!zoomActive || offset == 0) { + if (!_zoomActive || offset == 0) { return; } - cam.orthographicSize = Mathf.Clamp(cam.orthographicSize - (offset * speed), ZoomBounds[0], ZoomBounds[1]); + _cam.orthographicSize = Mathf.Clamp(_cam.orthographicSize - (offset * speed), ZoomBounds[0], ZoomBounds[1]); } void PanCamera(Vector3 newPanPosition) { - if (!panActive) { + if (!_panActive) { return; } // Translate the camera position based on the new input position - Vector3 offset = cam.ScreenToViewportPoint(lastPanPosition - newPanPosition); + Vector3 offset = _cam.ScreenToViewportPoint(_lastPanPosition - newPanPosition); Vector3 move = new Vector3(offset.x * PanSpeed, offset.y * PanSpeed, 0f); transform.Translate(move, Space.World); ClampToBounds(); - lastPanPosition = newPanPosition; + _lastPanPosition = newPanPosition; } void ClampToBounds() { diff --git a/td/Assets/Scripts/castle.cs b/td/Assets/Scripts/castle.cs new file mode 100644 index 0000000..5c8835c --- /dev/null +++ b/td/Assets/Scripts/castle.cs @@ -0,0 +1,23 @@ +using System.Collections; +using System.Collections.Generic; +using UnityEngine; + +public class Castle : MonoBehaviour { + + private void Update () { + //CheckThing(); + } + + private void CheckThing() { + GameObject[] enemies = GameObject.FindGameObjectsWithTag ("enemy"); + + foreach (var enemy in enemies) { + var distanceToEnemy = Vector3.Distance (transform.position, enemy.transform.position); + if (distanceToEnemy <= 0) { + Debug.Log("INTRUDER!"); + } + } + } + + +} diff --git a/td/Assets/Scripts/castle.cs.meta b/td/Assets/Scripts/castle.cs.meta new file mode 100644 index 0000000..246ac0b --- /dev/null +++ b/td/Assets/Scripts/castle.cs.meta @@ -0,0 +1,12 @@ +fileFormatVersion: 2 +guid: c6004dcebebd0498389086d43a3bd6fa +timeCreated: 1507302323 +licenseType: Free +MonoImporter: + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: diff --git a/td/Assets/Scripts/developerMode.cs b/td/Assets/Scripts/developerMode.cs index 4f241cf..a71d4c7 100644 --- a/td/Assets/Scripts/developerMode.cs +++ b/td/Assets/Scripts/developerMode.cs @@ -3,49 +3,49 @@ using System.Collections.Generic; using UnityEngine.UI; using UnityEngine; -public class developerMode : MonoBehaviour { +public class DeveloperMode : MonoBehaviour { - public string output = ""; - public string stack = ""; - public bool cheatsAllowed; + public string Output = ""; + public string Stack = ""; + public bool CheatsAllowed; - GameObject pnlCanvas; - GameObject pnlCheats; - Button btnToggleCheats; - Text lblConsoleLog; + GameObject _pnlCanvas; + GameObject _pnlCheats; + Button _btnToggleCheats; + Text _lblConsoleLog; - bool developerModeActive; - bool cheatMenuOpen; + bool _developerModeActive; + bool _cheatMenuOpen; void Start () { /* Panels */ - pnlCanvas = this.gameObject.transform.GetChild (0).gameObject; - pnlCheats = pnlCanvas.transform.Find ("cheatMenu").gameObject; + _pnlCanvas = this.gameObject.transform.GetChild (0).gameObject; + _pnlCheats = _pnlCanvas.transform.Find ("cheatMenu").gameObject; /* Buttons */ /* Button handlers */ /* Lablels */ - lblConsoleLog = pnlCanvas.transform.Find ("consoleLog").gameObject.GetComponent (); + _lblConsoleLog = _pnlCanvas.transform.Find ("consoleLog").gameObject.GetComponent (); /* Do setup */ - lblConsoleLog.text = ""; + _lblConsoleLog.text = ""; - if (cheatsAllowed) { - btnToggleCheats = pnlCanvas.transform.Find ("toggleCheats").gameObject.GetComponent