aboutsummaryrefslogtreecommitdiffstats
path: root/Assets/Scripts
diff options
context:
space:
mode:
authorcross28 <45377355+cross28@users.noreply.github.com>2022-04-15 06:30:32 -0500
committerGitHub <noreply@github.com>2022-04-15 06:30:32 -0500
commitce047b021d4d7d66ec57973d8e3b27a0f223c35e (patch)
tree891dc998c66b26d7d1df85c11d7c93c23ad852a5 /Assets/Scripts
parentee708901a74b9d442e2e8397b699a568928a8dc7 (diff)
parentb80e48181fb54dd80d4f344d8354e768200beb7f (diff)
Merge pull request #4 from MontanaJohns/feat/enemy-ai
Feat/enemy ai
Diffstat (limited to 'Assets/Scripts')
-rw-r--r--Assets/Scripts/Actors/Actor.cs16
-rw-r--r--Assets/Scripts/Actors/Enemy.cs57
-rw-r--r--Assets/Scripts/Actors/Enemy.cs.meta (renamed from Assets/Scripts/Weapon.cs.meta)2
-rw-r--r--Assets/Scripts/Actors/Player.cs8
-rw-r--r--Assets/Scripts/Projectile.cs4
-rw-r--r--Assets/Scripts/Weapon.cs23
6 files changed, 82 insertions, 28 deletions
diff --git a/Assets/Scripts/Actors/Actor.cs b/Assets/Scripts/Actors/Actor.cs
index 63854d9..f23f3c6 100644
--- a/Assets/Scripts/Actors/Actor.cs
+++ b/Assets/Scripts/Actors/Actor.cs
@@ -68,10 +68,22 @@ namespace MontanaJohns.Actors
_rigidBody.velocity = Vector2.SmoothDamp(_rigidBody.velocity, target, ref acceleration, .05f);
_animator.SetBool("moving", Mathf.Abs(_rigidBody.velocity.x) > 1);
- if (_rigidBody.velocity.x < -0.001)
+ if (_rigidBody.velocity.x < -0.001) {
_renderer.flipX = true;
- else if (_rigidBody.velocity.x > 0.001)
+ for (int i = 0; i < transform.childCount; i++) {
+ var rot = transform.GetChild(i).rotation;
+ rot.y = 180f;
+ transform.GetChild(i).rotation = rot;
+ }
+ }
+ else if (_rigidBody.velocity.x > 0.001) {
_renderer.flipX = false;
+ for (int i = 0; i < transform.childCount; i++) {
+ var rot = transform.GetChild(i).rotation;
+ rot.y = 0f;
+ transform.GetChild(i).rotation = rot;
+ }
+ }
}
public virtual void MoveY(float input)
{
diff --git a/Assets/Scripts/Actors/Enemy.cs b/Assets/Scripts/Actors/Enemy.cs
new file mode 100644
index 0000000..9160e44
--- /dev/null
+++ b/Assets/Scripts/Actors/Enemy.cs
@@ -0,0 +1,57 @@
+using UnityEngine;
+using System.Collections;
+
+namespace MontanaJohns.Actors
+{
+ [RequireComponent(typeof(Rigidbody2D))]
+ public class Enemy : Actor
+ {
+ protected GameObject player;
+ protected float attackRate = 0.5f;
+ protected float nextAttackTime = 0f;
+
+ protected override void Awake()
+ {
+ base.Awake();
+ player = GameObject.FindGameObjectWithTag("Player");
+ }
+
+ // Update is called once per frame
+ void Update()
+ {
+ MoveTowardsPlayer();
+ Attack();
+ CheckHealth();
+ }
+
+ void MoveTowardsPlayer()
+ {
+ if (player.transform.position.x < transform.position.x) Move(-stats.speedMultiplier * 0.5f);
+ else Move(stats.speedMultiplier * 0.5f);
+ }
+
+ void CheckHealth()
+ {
+ if (health <= 0) Destroy(gameObject);
+ }
+
+ void Attack()
+ {
+ if (Mathf.Abs(player.transform.position.x - transform.position.x) <= 5 && Time.time >= nextAttackTime) {
+ _animator.SetTrigger("attack");
+ player.GetComponent<Actor>().TakeDamage(1);
+ nextAttackTime = Time.time + 1f / attackRate;
+ }
+ }
+
+ private void OnCollisionEnter2D(Collision2D other)
+ {
+ if (other.gameObject.tag == "Projectile")
+ {
+ Debug.Log("Enemy: Hit by projectile");
+ TakeDamage(1);
+ Destroy(other.gameObject);
+ }
+ }
+ }
+}
diff --git a/Assets/Scripts/Weapon.cs.meta b/Assets/Scripts/Actors/Enemy.cs.meta
index 6546dd7..26a1d77 100644
--- a/Assets/Scripts/Weapon.cs.meta
+++ b/Assets/Scripts/Actors/Enemy.cs.meta
@@ -1,5 +1,5 @@
fileFormatVersion: 2
-guid: 2b8de137e60a3cb418e266e99100d25f
+guid: 6751c31468656894092c67dc838ac9a6
MonoImporter:
externalObjects: {}
serializedVersion: 2
diff --git a/Assets/Scripts/Actors/Player.cs b/Assets/Scripts/Actors/Player.cs
index 1b2155a..95ec2ca 100644
--- a/Assets/Scripts/Actors/Player.cs
+++ b/Assets/Scripts/Actors/Player.cs
@@ -10,6 +10,8 @@ namespace MontanaJohns.Actors
{
public Transform ActorTransform => _transform;
public Camera MainCamera => _camera;
+ public GameObject projectilePrefab;
+ public Transform firePoint;
public Vector3 spawnPoint;
private Camera _camera;
@@ -28,6 +30,7 @@ namespace MontanaJohns.Actors
jump.started += context => Jump();
use.started += context => Use();
+ use.started += context => Fire();
}
protected override void Start()
@@ -68,5 +71,10 @@ namespace MontanaJohns.Actors
{
stats = baseStats + activeItem.stats;
}
+
+ protected void Fire()
+ {
+ Instantiate(projectilePrefab, firePoint.position, firePoint.rotation);
+ }
}
} \ No newline at end of file
diff --git a/Assets/Scripts/Projectile.cs b/Assets/Scripts/Projectile.cs
index 3b86e4a..7692917 100644
--- a/Assets/Scripts/Projectile.cs
+++ b/Assets/Scripts/Projectile.cs
@@ -1,6 +1,7 @@
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
+using MontanaJohns.Actors;
public class Projectile : MonoBehaviour
{
@@ -13,9 +14,8 @@ public class Projectile : MonoBehaviour
rb.velocity = transform.right * speed;
}
- private void OnTriggerEnter2D(Collider2D collision)
+ private void OnCollisionEnter2D(Collision2D collision)
{
- Debug.Log(collision.name);
Destroy(gameObject);
}
}
diff --git a/Assets/Scripts/Weapon.cs b/Assets/Scripts/Weapon.cs
deleted file mode 100644
index 1af1abb..0000000
--- a/Assets/Scripts/Weapon.cs
+++ /dev/null
@@ -1,23 +0,0 @@
-using System.Collections;
-using System.Collections.Generic;
-using UnityEngine;
-
-public class Weapon : MonoBehaviour
-{
- public Transform firePoint;
- public GameObject projectilePrefab;
-
- // Update is called once per frame
- void Update()
- {
- if (Input.GetButtonDown("Fire1"))
- {
- Shoot();
- }
- }
-
- void Shoot()
- {
- Instantiate(projectilePrefab, firePoint.position, firePoint.rotation);
- }
-}