1 | using System; |
---|
2 | using System.Collections.Generic; |
---|
3 | using System.Linq; |
---|
4 | using System.Text; |
---|
5 | using CastleMaster.Entities; |
---|
6 | using CastleMaster.World; |
---|
7 | using CastleMaster.Players; |
---|
8 | using CastleMaster.Physics; |
---|
9 | using Microsoft.Xna.Framework; |
---|
10 | using CastleMaster.Graphics; |
---|
11 | |
---|
12 | namespace CastleMaster.Units |
---|
13 | { |
---|
14 | public class Unit : Entity |
---|
15 | { |
---|
16 | protected bool isSelectable, isSelected; |
---|
17 | protected BoundingRectangle screenRect; |
---|
18 | protected BoundingRectangle screenRectOffset; |
---|
19 | protected Vector2 highlightOffset = Vector2.Zero; |
---|
20 | protected Point spriteSize; |
---|
21 | private float timer = -MathHelper.TwoPi, arrowSpeed = 0.1F; |
---|
22 | private Vector2 arrowOffs = Vector2.Zero; |
---|
23 | private Player owner; |
---|
24 | |
---|
25 | public Unit(Level level, Player owner) |
---|
26 | : base(level) |
---|
27 | { |
---|
28 | this.owner = owner; |
---|
29 | |
---|
30 | spriteSize = new Point(32, 32); |
---|
31 | screenRectOffset = new BoundingRectangle(0, 0, spriteSize.X, spriteSize.Y, null); |
---|
32 | isSelectable = true; |
---|
33 | isSelected = false; |
---|
34 | } |
---|
35 | |
---|
36 | public bool IsSelectable { get { return isSelectable; } } |
---|
37 | |
---|
38 | public Player Owner { get { return owner; } } |
---|
39 | |
---|
40 | public override void Init() |
---|
41 | { |
---|
42 | base.Init(); |
---|
43 | } |
---|
44 | |
---|
45 | public virtual void OnSelectGain() |
---|
46 | { |
---|
47 | isSelected = true; |
---|
48 | } |
---|
49 | |
---|
50 | public virtual void OnSelectLost() |
---|
51 | { |
---|
52 | isSelected = false; |
---|
53 | } |
---|
54 | |
---|
55 | public virtual void OnFunctionClick() { } |
---|
56 | |
---|
57 | public override void Update() |
---|
58 | { |
---|
59 | if (isSelected) |
---|
60 | { |
---|
61 | timer += arrowSpeed; |
---|
62 | if (timer >= MathHelper.TwoPi) timer = -MathHelper.TwoPi; |
---|
63 | arrowOffs.Y = (float)Math.Sin(timer) * 2.0F; |
---|
64 | } |
---|
65 | } |
---|
66 | |
---|
67 | public bool IntersectsWithScreenSpace(float x0, float y0, float x1, float y1) |
---|
68 | { |
---|
69 | if (!isSelectable) return false; |
---|
70 | screenRect = new BoundingRectangle(screenPos.X, screenPos.Y, screenPos.X, screenPos.Y, this).AddSelf(screenRectOffset).Scale(Viewport.ZOOM); |
---|
71 | return screenRect.Intersects(x0, y0, x1, y1); |
---|
72 | } |
---|
73 | |
---|
74 | public float DistanceTo(float x, float z) |
---|
75 | { |
---|
76 | float xd = X - x; |
---|
77 | float zd = Z - z; |
---|
78 | |
---|
79 | return (float)(Math.Sqrt(xd * xd + zd * zd)); |
---|
80 | } |
---|
81 | |
---|
82 | public float DistanceFromScreenSpaceSqr(float x, float y) |
---|
83 | { |
---|
84 | float dx = screenPos.X * Viewport.ZOOM - x; |
---|
85 | float dy = screenPos.Y * Viewport.ZOOM - y; |
---|
86 | |
---|
87 | return dx * dx + dy * dy; |
---|
88 | } |
---|
89 | |
---|
90 | public virtual void RenderHighLight(RenderHelper renderer) |
---|
91 | { |
---|
92 | renderer.Render((screenPos + highlightOffset + arrowOffs) * Viewport.ZOOM, 1, 0, Resources.SPRITESHEET_ICONS, Viewport.ZOOM); |
---|
93 | } |
---|
94 | |
---|
95 | public override void Render(RenderHelper renderer) |
---|
96 | { |
---|
97 | if (isSelected) |
---|
98 | RenderHighLight(renderer); |
---|
99 | } |
---|
100 | } |
---|
101 | } |
---|