summaryrefslogtreecommitdiff
path: root/scripts/Entities/Actions/SpellAction.cs
blob: d9e99526b321a01ba1fa83f33dd979398993c7bf (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
using Godot;
using TheLegendOfGustav.Entities.Actors;
using TheLegendOfGustav.Magic;
using TheLegendOfGustav.Utils;

namespace TheLegendOfGustav.Entities.Actions;

/// <summary>
/// Ação para quando o jogador joga um feitiço.
/// </summary>
public partial class SpellAction : DirectionalAction
{
	private SpellResource spell;

	public SpellAction(Actor actor, Vector2I offset, SpellResource spell) : base(actor, offset)
	{
		this.spell = spell;

		Cost = 5;
	}

	public override bool Perform()
	{
		Actor target;

		if (spell.Type == SpellType.Self)
		{
			target = Actor;
		}
		else if (GetTarget() is Actor actor)
		{
			target = actor;
		}
		else
		{
			return false;
		}

		if (Grid.Distance(Actor.GridPosition, target.GridPosition) > spell.Range)
		{
			return false;
		}

		if (Actor.Mp < spell.Cost)
		{
			return false;
		}

		Actor.Mp -= spell.Cost;

		foreach (SpellEffect effect in spell.Effects)
		{
			effect.Apply(Actor, target);
		}

		Actor.Energy -= Cost;
		return true;
	}
}