/
Character.cs
188 lines (156 loc) · 5.63 KB
/
Character.cs
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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
namespace ConsoleApplication3
{
public class Character
{
public Faction Faction;
public CharacterGroup Party;
public string Name;
public Stats Stats = new Stats();
public Vector2 Position;
//public int Distance;
public float AP;
public string LastAction = string.Empty;
public bool IsAlive;
public Character currentTarget;
//private Action2 currentAction;
private int currentAction = -1;
public List<Action2> ActionList = new List<Action2>();
public void Turn(Level level, float dt)
{
//check if ability is still valid
if (currentAction > -1 && !ActionList[currentAction].Ability.CanUse(level, this, currentTarget))
{
//cancel
currentAction = -1;
AP = 0;
currentTarget = null;
}
//will check all actions or the 1 up to the currentAction (for canceling)
var maxAction = currentAction == -1 ? ActionList.Count : currentAction;
for (var i = 0; i < maxAction; i++)
{
//found either a new action to run or higher priority action
if (ActionList[i].Check(level, this))
{
if (currentAction > -1)
{
//cancel (don't clear target because Check set the new one)
}
currentAction = i;
AP = 0;
break;
}
}
if (currentAction > -1)
{
var action = ActionList[currentAction];
var ability = action.Ability;
LastAction = ability.Name;
var distance = Vector2.Distance(Position, currentTarget.Position);
if (distance > ability.MaxDistance)
{
//move closer
var dir = (currentTarget.Position - Position);
dir.Normalize();
dir *= (dt * 100);
Position += dir;
}
else
{
AP += ability.ChargeRate * APRechargeRate * dt;
if (AP >= 100)
{
ability.Use(level, this, currentTarget);
AP = 0;
currentTarget = null;
currentAction = -1;
Turn(level, dt);
}
}
}
else
{
LastAction = "Idle";
}
}
public void TakeDamage(int damage)
{
if (IsAlive)
{
HP -= damage;
if (HP < 0)
{
//Console.WriteLine("{0} has died!", Name);
IsAlive = false;
}
}
}
public void HealDamage(int damage)
{
if (IsAlive)
{
HP += damage;
if (HP > MaxHP)
HP = MaxHP;
}
}
public float HP
{
get { return Stats.GetBaseValue("hp_now"); }
set { Stats.SetBaseStat("hp_now", value); }
}
public float MaxHP
{
get { return Stats.GetBaseValue("hp_max"); }
//set { Stats.SetBaseStat("hp_max", value); }
}
public float AttackDamage
{
get { return Stats.GetModifiedValue("attack_damage") * 2; }
//set { Stats.SetBaseStat("attack_damage", value);}
}
public float APRechargeRate { get { return Stats.GetModifiedValue("speed"); } }
public int APOver10 { get { return (int)(AP / 10); } }
public int APOver100 { get { return (int)(AP / 100); } }
public int HPPercent { get { return (int)(100 * ((float)HP / (float)MaxHP)); } }
public int Size { get { return 10; } }
public float Left { get { return Position.X - Size / 2; } }
public float Right { get { return Position.X + Size / 2; } }
public float Top { get { return Position.Y + Size / 2; } }
public float Bottom { get { return Position.Y - Size / 2; } }
public bool IsAlly(Character other)
{
return !Faction.IsHostile(other.Faction);
}
public void Draw(SpriteBatch batch)
{
if (IsAlive)
{
var color = Faction == Faction.Ally ? Color.Blue : Color.Red;
batch.DrawRect(Position, 20, color);
if (currentTarget != null)
{
if (!currentTarget.Faction.IsHostile(Faction))
color = Color.Green;
var dir = currentTarget.Position - Position;
//dir.Normalize();
dir *= (AP / 100);
batch.DrawLine(Position, Position + dir, color, 2);
}
}
}
public override string ToString()
{
if (IsAlive)
return string.Format("{0,10} - HP: {1,3} {7,3}%, AD: {2,3}, AR: {6,2}, AP: [{3}{4}] {5}", Name, HP, AttackDamage, new string('#', APOver10), new string(' ', 10 - APOver10), LastAction, APRechargeRate, HPPercent);
return string.Format("{0,10} - Dead!", Name);
}
}
}