// Update is called once per frame void Update() { if (GameController.CurrentGamePlayStatus != GameController.GameplayStatus.Play) { return; } float h = Input.GetAxis("Horizontal"); float v = Input.GetAxis("Vertical"); Vector3 direction = this.transform.localScale; if ((h != 0 || v != 0) && Input.GetKeyUp(KeyCode.Z) == false) { SetTrigger("Mover"); } if (h < 0 && this.CurrentLookingTo != LookingTo.Left) { this.CurrentLookingTo = LookingTo.Left; //this.gameObject.GetComponent<Renderer>().material.color = Color.blue; transform.localEulerAngles = new Vector3(0, -90, 0); //direction = new Vector3(this.transform.localScale.x*-1, 1, 1); //this.transform.localScale = direction; } else if (h > 00 && this.CurrentLookingTo != LookingTo.Right) { this.CurrentLookingTo = LookingTo.Right; //this.gameObject.GetComponent<Renderer>().material.color = Color.red; transform.localEulerAngles = new Vector3(0, 90, 0); //direction = new Vector3(this.transform.localScale.x*-1,1,1); //this.transform.localScale = direction; } Vector3 dest = this.transform.position; dest.x += h; dest.z += v; //Debug.LogFormat("Moving To: {0},{1}, {2}", dest.x, dest.y, dest.z); this.transform.position = Vector3.MoveTowards(this.transform.position, dest, Speed * Time.deltaTime); var fwd = transform.rotation * Vector3.forward; Debug.DrawRay(this.transform.position, fwd, Color.black); if (Input.GetKeyUp(KeyCode.Z)) { SetTrigger("Atacar"); EnemyHunger closestEnemy = this.EnemyController.SpawnedEnemies.OrderBy(d => d.GetDistanceToTarget()).FirstOrDefault(); if (closestEnemy != null && closestEnemy.IsValidTarget) { ApplyAttack(fwd, closestEnemy.gameObject, closestEnemy); } Ray newRay = new Ray(this.transform.position, fwd); RaycastHit raycastHitInfo = new RaycastHit(); //AttackByRaycasting(ref fwd, ref newRay, ref raycastHitInfo); //Debug.LogFormat("{0},{1}, {2}", fwd.x, fwd.y, fwd.z); //this.transform.position += fwd; } }
public void OnTriggerExit(Collider other) { if (other.gameObject != null) { EnemyHunger enemyHungerComponent = other.gameObject.GetComponent <EnemyHunger>(); Debug.LogFormat("Collided: {0}", other.name); if (enemyHungerComponent != null) { this.EnemyController.SetValidTarget(enemyHungerComponent, false); } } }
internal void DefeatEnemy(EnemyHunger enemyHunger) { int indexOfEnemy = this.SpawnedEnemies.IndexOf(enemyHunger); Debug.LogFormat("Index of enemy: {0}", indexOfEnemy); if (indexOfEnemy >= 0) { this.PlayerScoreSystem.AddToScore(enemyHunger.PointsPerDefeat); GameObject.Destroy(enemyHunger.gameObject); this.SpawnedEnemies.RemoveAt(indexOfEnemy); this.PlayerScoreSystem.EnemiesDefeated++; } }
private void AttackByRaycasting(ref Vector3 fwd, ref Ray newRay, ref RaycastHit raycastHitInfo) { bool willCollide = Physics.Raycast(newRay, out raycastHitInfo); if (willCollide) { if (raycastHitInfo.collider != null) { var collidedGameObject = raycastHitInfo.collider.gameObject; EnemyHunger enemyHungerComponent = collidedGameObject.GetComponent <EnemyHunger>(); ApplyAttack(fwd, collidedGameObject, enemyHungerComponent); } } else { Debug.Log("Nothing in front"); } }
// Update is called once per frame void Update() { if (GameController.CurrentGamePlayStatus != GameController.GameplayStatus.Play) { return; } if (Time.time > this.lastTimeEnemySpawned + BaseSpawnInternal && this.SpawnedEnemies.Count < this.MaxAllowedEnemiesInScene) { this.lastTimeEnemySpawned = Time.time; int totalSpawnPoints = this.SpawnPoints.Count; int spawnPointIndex = Random.Range(0, totalSpawnPoints - 1); GameObject newEnemy = Instantiate <GameObject>(Resources.Load <GameObject>("Prefabs/Enemy_Hunger")); newEnemy.transform.position = this.SpawnPoints[spawnPointIndex].transform.position; EnemyHunger enemyHungerComponent = newEnemy.GetComponent <EnemyHunger>(); enemyHungerComponent.Target = this.Player; this.SpawnedEnemies.Add(enemyHungerComponent); //newEnemy.name = "asdadasd"; } }
private void ApplyAttack(Vector3 fwd, GameObject collidedGameObject, EnemyHunger enemyHungerComponent) { if (enemyHungerComponent != null) { Rigidbody enemyRigidBody = collidedGameObject.GetComponent <Rigidbody>(); enemyRigidBody.AddForce(fwd * ImpulseForceOnEnemy, ForceMode.Impulse); Debug.LogFormat("Can attack: {0}", collidedGameObject.name); int enemyCurrentHP = enemyHungerComponent.CurrentStats.HealthPoints; enemyCurrentHP -= this.AttackDamage; if (enemyCurrentHP < 0) { enemyCurrentHP = 0; } enemyHungerComponent.CurrentStats.HealthPoints = enemyCurrentHP; if (enemyHungerComponent.CurrentStats.HealthPoints == 0) { this.EnemyController.DefeatEnemy(enemyHungerComponent); } } }
internal void SetValidTarget(EnemyHunger enemyHungerComponent, bool isValidTarget) { enemyHungerComponent.IsValidTarget = isValidTarget; }