1 | using CastleMaster.Entities; |
---|
2 | using CastleMaster.Graphics; |
---|
3 | using CastleMaster.Physics; |
---|
4 | using CastleMaster.Players; |
---|
5 | using CastleMaster.World; |
---|
6 | using Microsoft.Xna.Framework; |
---|
7 | using System; |
---|
8 | |
---|
9 | namespace CastleMaster.Units |
---|
10 | { |
---|
11 | public class Unit : Entity |
---|
12 | { |
---|
13 | protected bool isSelectable; |
---|
14 | protected BoundingRectangle screenRect; |
---|
15 | protected BoundingRectangle screenRectOffset; |
---|
16 | protected Vector2 highlightOffset = Vector2.Zero; |
---|
17 | protected Point spriteSize; |
---|
18 | private float timer = -MathHelper.TwoPi, arrowSpeed = 0.1F; |
---|
19 | private Vector2 arrowOffs = Vector2.Zero; |
---|
20 | private Player owner; |
---|
21 | private int immunityTimer = 0; |
---|
22 | protected int maxHealth, immunityTime; |
---|
23 | protected Color colorizer = Color.White; |
---|
24 | protected bool colorizeOnHit, wasHurt = false; |
---|
25 | |
---|
26 | public Unit(Level level, Player owner) |
---|
27 | : base(level) |
---|
28 | { |
---|
29 | this.owner = owner; |
---|
30 | |
---|
31 | spriteSize = new Point(32, 32); |
---|
32 | screenRectOffset = new BoundingRectangle(0, 0, spriteSize.X, spriteSize.Y, null); |
---|
33 | isSelectable = true; |
---|
34 | IsSelected = false; |
---|
35 | HasHealth = false; |
---|
36 | maxHealth = 10; |
---|
37 | colorizeOnHit = true; |
---|
38 | immunityTime = 0; |
---|
39 | } |
---|
40 | |
---|
41 | public int MaxHealth { get { return maxHealth; } } |
---|
42 | |
---|
43 | public int Health { get; protected set; } |
---|
44 | |
---|
45 | public bool HasHealth { get; protected set; } |
---|
46 | |
---|
47 | public bool IsSelectable { get { return isSelectable; } } |
---|
48 | |
---|
49 | public Player Owner { get { return owner; } } |
---|
50 | |
---|
51 | public bool IsSelected { get; set; } |
---|
52 | |
---|
53 | public override void Init() |
---|
54 | { |
---|
55 | base.Init(); |
---|
56 | Health = maxHealth; |
---|
57 | } |
---|
58 | |
---|
59 | public virtual void OnFocus() { } |
---|
60 | |
---|
61 | public virtual void OnFocusLost() { } |
---|
62 | |
---|
63 | public virtual bool CanBeHurtBy(Unit u) |
---|
64 | { |
---|
65 | return HasHealth && u.Owner != Owner; |
---|
66 | } |
---|
67 | |
---|
68 | public void Hit(Unit u, int damage, float dir, float pushPower) |
---|
69 | { |
---|
70 | if (u.CanBeHurtBy(this)) |
---|
71 | u.Damage(this, damage, dir, pushPower); |
---|
72 | } |
---|
73 | |
---|
74 | public virtual void Damage(Unit attacker, int damage, float dir, float pushPower) |
---|
75 | { |
---|
76 | Health -= damage; |
---|
77 | if (colorizeOnHit) |
---|
78 | colorizer = Color.Red; |
---|
79 | if (immunityTime > 0) |
---|
80 | { |
---|
81 | wasHurt = true; |
---|
82 | HasHealth = false; |
---|
83 | } |
---|
84 | if (Health <= 0) |
---|
85 | Remove(); |
---|
86 | } |
---|
87 | |
---|
88 | public virtual void OnFunctionClick(float x, float z, int sameOrderAmount, bool wasCalledBefore) { } |
---|
89 | |
---|
90 | public override void Update() |
---|
91 | { |
---|
92 | base.Update(); |
---|
93 | |
---|
94 | if (IsSelected) |
---|
95 | { |
---|
96 | timer += arrowSpeed; |
---|
97 | if (timer >= MathHelper.TwoPi) timer = -MathHelper.TwoPi; |
---|
98 | arrowOffs.Y = (float)Math.Sin(timer) * 2.0F; |
---|
99 | } |
---|
100 | |
---|
101 | if (wasHurt) |
---|
102 | { |
---|
103 | immunityTimer++; |
---|
104 | if (immunityTimer >= immunityTime) |
---|
105 | { |
---|
106 | wasHurt = false; |
---|
107 | immunityTimer = 0; |
---|
108 | colorizer = Color.White; |
---|
109 | HasHealth = true; |
---|
110 | } |
---|
111 | } |
---|
112 | } |
---|
113 | |
---|
114 | public override void OnRemoved() |
---|
115 | { |
---|
116 | level.RemoveUnit(this); |
---|
117 | } |
---|
118 | |
---|
119 | public bool IntersectsWithScreenSpace(float x0, float y0, float x1, float y1) |
---|
120 | { |
---|
121 | if (!isSelectable) return false; |
---|
122 | screenRect = new BoundingRectangle(screenPos.X, screenPos.Y, screenPos.X, screenPos.Y, this).AddSelf(screenRectOffset).Scale(Viewport.ZOOM); |
---|
123 | return screenRect.Intersects(x0, y0, x1, y1); |
---|
124 | } |
---|
125 | |
---|
126 | public float DistanceFromScreenSpaceSqr(float x, float y) |
---|
127 | { |
---|
128 | float dx = screenPos.X * Viewport.ZOOM - x; |
---|
129 | float dy = screenPos.Y * Viewport.ZOOM - y; |
---|
130 | |
---|
131 | return dx * dx + dy * dy; |
---|
132 | } |
---|
133 | |
---|
134 | public virtual void RenderHighLight(RenderHelper renderer) |
---|
135 | { |
---|
136 | renderer.Render((screenPos + highlightOffset + arrowOffs) * Viewport.ZOOM, 1, 0, Resources.SPRITESHEET_ICONS, Viewport.ZOOM); |
---|
137 | } |
---|
138 | |
---|
139 | public override void Render(RenderHelper renderer) |
---|
140 | { |
---|
141 | if (IsSelected) |
---|
142 | RenderHighLight(renderer); |
---|
143 | } |
---|
144 | } |
---|
145 | } |
---|