-
Notifications
You must be signed in to change notification settings - Fork 35
/
PlayerModel.cs
102 lines (81 loc) · 2.18 KB
/
PlayerModel.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
using System;
namespace SocialPoint.Examples.MVC
{
public class PlayerModel
{
public PlayerModel()
{
XP = 0;
Level = 1;
HitPoints = MaxHitPoints;
}
public event EventHandler XPGained;
public event EventHandler LevelUp;
public event EventHandler DamageTaken;
public event EventHandler Died;
int _hitPoints;
public int HitPoints
{
get
{
return _hitPoints;
}
set
{
var oldValue = _hitPoints;
_hitPoints = value;
if (_hitPoints < 0)
{
_hitPoints = 0;
}
if (oldValue != _hitPoints)
{
if (DamageTaken != null) DamageTaken(this, EventArgs.Empty);
if (IsDead)
{
if (Died != null) Died(this, EventArgs.Empty);
}
}
}
}
public int Level { get; private set;}
public int XP {get; private set;}
public int MaxHitPoints
{
get { return Level * 150; }
}
public bool HasLowHitPoints
{
get { return HitPoints <= MaxHitPoints * 0.25f; }
}
public bool IsDead
{
get { return HitPoints <= 0;}
}
public void TakeDamage(int hpDamage)
{
if (IsDead) return;
HitPoints -= hpDamage;
}
public void AddXp(int amount)
{
if (IsDead) return;
XP += amount;
OnPlayerXPGained();
if (XP >= (100 * Level))
{
OnPlayerLevelUp();
}
}
protected virtual void OnPlayerLevelUp()
{
Level++;
HitPoints = MaxHitPoints;
if (LevelUp != null) LevelUp(this, EventArgs.Empty);
}
protected virtual void OnPlayerXPGained()
{
if (XPGained != null) XPGained(this, EventArgs.Empty);
}
}
}