Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Run CSharpier on Assets folder #27

Merged
merged 1 commit into from
Mar 7, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
26 changes: 12 additions & 14 deletions Assets/Scripts/Characters/DamageText.cs
Original file line number Diff line number Diff line change
@@ -1,46 +1,44 @@
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

using TMPro;
using DG.Tweening;

using TMPro;
using UnityEngine;

public class DamageText : MonoBehaviour
{
private TextMeshPro _text;

void Awake()
{
_text = transform.Find("Text").GetComponent<TextMeshPro>();
}

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

}
void Update() { }

public void SetDamage(int damage)
{
_text.text = damage.ToString();
}

private void Init(int value) {

private void Init(int value)
{
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

as god intended

// set the text
this.SetDamage(value);

// make the text jump up
transform.DOLocalJump(transform.localPosition,
transform.DOLocalJump(
transform.localPosition,
1.0f, // jump power
1, // num jumps
0.5f // duration
1, // num jumps
0.5f // duration
);
}

// usage: _damageTextPrefab.Spawn(transform, position, 10, 2.0f);

public DamageText Spawn(Transform parent, Vector2 position, int value, float duration = 1.0f) {
public DamageText Spawn(Transform parent, Vector2 position, int value, float duration = 1.0f)
{
// instantiate the damage text prefab
var damageText = Instantiate(this, parent);
damageText.transform.localPosition = position;
Expand Down
5 changes: 2 additions & 3 deletions Assets/Scripts/Characters/DeathEnemy.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,15 @@

public class DeathEnemy : Enemy
{
override protected Vector2 DetermineDirection(GameObject player)
protected override Vector2 DetermineDirection(GameObject player)
{
// get direction facing player
Vector2 direction = Player.Instance.transform.position - transform.position;

return Vector3.Normalize(direction);
}


override protected void OnCollisionEnter2D(Collision2D collision)
protected override void OnCollisionEnter2D(Collision2D collision)
{
base.OnCollisionEnter2D(collision);

Expand Down
9 changes: 5 additions & 4 deletions Assets/Scripts/Characters/DiagonalEnemy.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,19 +7,20 @@ public class DiagonalEnemy : Enemy
{
Vector2 _direction;

override protected void Awake() {
protected override void Awake()
{
base.Awake();

// initialize a random direction
_direction = new Vector3(Random.Range(-1.0f, 1.0f), Random.Range(-1.0f, 1.0f));

}
override protected Vector2 DetermineDirection(GameObject player)

protected override Vector2 DetermineDirection(GameObject player)
{
return Vector3.Normalize(_direction);
}

override protected void OnCollisionEnter2D(Collision2D collision)
protected override void OnCollisionEnter2D(Collision2D collision)
{
base.OnCollisionEnter2D(collision);

Expand Down
79 changes: 45 additions & 34 deletions Assets/Scripts/Characters/Enemy.cs
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

using DG.Tweening;
using UnityEngine;

public class Enemy : MonoBehaviour
{
Expand All @@ -21,7 +20,7 @@ public class Enemy : MonoBehaviour
[SerializeField]
[Tooltip("How much XP the player earns")]
private int _xpValue = 10;

[SerializeField]
protected float _speed = 1f;

Expand Down Expand Up @@ -51,7 +50,8 @@ public class Enemy : MonoBehaviour

protected Rigidbody2D _rigidbody2D;

virtual protected void Awake() {
protected virtual void Awake()
{
_spriteRenderer = GetComponent<SpriteRenderer>();
_rigidbody2D = GetComponent<Rigidbody2D>();

Expand All @@ -60,7 +60,7 @@ public class Enemy : MonoBehaviour

// Update is called once per frame
protected void Update()
{
{
// move towards the player character
GameObject player = GameObject.Find("Player");
if (player != null)
Expand All @@ -76,10 +76,11 @@ protected void Update()
/**
* Returns a normalized vector in the direction of the desired movement
*/
virtual protected Vector2 DetermineDirection(GameObject player) {
virtual protected Vector2 DetermineDirection(GameObject player)
{
return Vector3.Normalize(player.transform.position - transform.position);
}

// a collision handler that is called when the enemy collides with another object
virtual protected void OnCollisionEnter2D(UnityEngine.Collision2D collision)
{
Expand All @@ -91,77 +92,87 @@ virtual protected void OnCollisionEnter2D(UnityEngine.Collision2D collision)

// Destroy the enemy (for now they explode if they touch the player)
Death();

}
}

// material flash trick from: https://www.youtube.com/watch?v=9rZkiEyS66I
public void Flash() {

if (_flashCoroutine != null) {
// material flash trick from: https://www.youtube.com/watch?v=9rZkiEyS66I
public void Flash()
{
if (_flashCoroutine != null)
{
StopCoroutine(_flashCoroutine);
}
_flashCoroutine = StartCoroutine(FlashCoroutine());
}

private IEnumerator FlashCoroutine() {
private IEnumerator FlashCoroutine()
{
_spriteRenderer.material = _flashMaterial;
yield return new WaitForSeconds(_flashDuration);
_spriteRenderer.material = _originalMaterial;
}

public void Knockback(Vector2 direction, float force) {
public void Knockback(Vector2 direction, float force)
{
_rigidbody2D.AddForce(direction * force);
}

virtual public void Death(bool leaveXp = false) {

public virtual void Death(bool leaveXp = false)
{
// disable all colliders and hitboxes
// -- that way enemy can't get hit again while they're dying/shrinking
DisableHitboxes();

// shrink (scale to 1) before being destroyed
transform.DOScale(0.01f, _deathAnimDuration).OnComplete(() => {
if (leaveXp) {
// instantiate an xp drop at this position
var xpDrop = Instantiate(_xpDropPrefab, transform.position, Quaternion.identity);
xpDrop.SetXp(_xpValue);
}
Destroy(gameObject);
});
transform
.DOScale(0.01f, _deathAnimDuration)
.OnComplete(() =>
{
if (leaveXp)
{
// instantiate an xp drop at this position
var xpDrop = Instantiate(
_xpDropPrefab,
transform.position,
Quaternion.identity
);
xpDrop.SetXp(_xpValue);
}
Destroy(gameObject);
});
}

protected void DisableHitboxes() {
protected void DisableHitboxes()
{
var collider = GetComponent<Collider2D>();
collider.enabled = false;

// get hitbox and disable
var hitbox = GetComponentInChildren<Hitbox>();
hitbox.Disable();

}

// Deal damage to the player because they touched
private void DamagePlayer(Player player) {
private void DamagePlayer(Player player)
{
Debug.Log("Enemy.DamagePlayer: Player was damaged by " + gameObject.name);

player.TakeDamage(_collisionDamage);
}

public void TakeDamage(int damage) {
public void TakeDamage(int damage)
{
Flash();

hitpoints -= damage;
hitpoints = Mathf.Max(hitpoints, 0); // don't let the enemy have negative hit points

// spawn the damage text above the enemy by 1 unit (32px)
var damageTextPosition = new Vector2(
transform.position.x,
transform.position.y + 1.0f
);
var damageTextPosition = new Vector2(transform.position.x, transform.position.y + 1.0f);
_damageTextPrefab.Spawn(transform.root, damageTextPosition, damage);

if (hitpoints == 0) {

if (hitpoints == 0)
{
Death(leaveXp: true);

EventManager.TriggerEvent("EnemyDestroyed", new EventData(_scoreValue));
Expand Down
7 changes: 2 additions & 5 deletions Assets/Scripts/Characters/HealthBar.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,19 +5,16 @@

public class HealthBar : MonoBehaviour
{

private Slider _slider;

// Start is called before the first frame update
void Start()
{
_slider = GetComponent<Slider>();
}

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

}
void Update() { }

// 0.0f = 0% health, 1.0f = 100% health
public void SetHealth(float health)
Expand Down
7 changes: 4 additions & 3 deletions Assets/Scripts/Characters/Hitbox.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,14 @@ public class Hitbox : MonoBehaviour

public Enemy Enemy => _parentEnemy;

void Awake() {
void Awake()
{
_parentEnemy = transform.parent.GetComponent<Enemy>();
}

public void Disable() {
public void Disable()
{
// disable collider
GetComponent<Collider2D>().enabled = false;
}

}