1 | using System; |
---|
2 | using System.Collections.Generic; |
---|
3 | using System.Linq; |
---|
4 | using System.Text; |
---|
5 | using CastleMaster.Ai.Orders; |
---|
6 | using CastleMaster.World; |
---|
7 | using CastleMaster.Players; |
---|
8 | using Microsoft.Xna.Framework; |
---|
9 | using IsometricEngineTest.Ai; |
---|
10 | using IsometricEngineTest.Ai.Orders; |
---|
11 | using CastleMaster.Graphics; |
---|
12 | using CastleMaster.Entities; |
---|
13 | |
---|
14 | namespace CastleMaster.Units.Mobs |
---|
15 | { |
---|
16 | public class Mob : Unit |
---|
17 | { |
---|
18 | private Order currentOrder; |
---|
19 | private float dir = 0.0F; |
---|
20 | private AStar pathFinder; |
---|
21 | protected int dirID; |
---|
22 | protected AnimationHelper walkingAnimation; |
---|
23 | protected float pushResistance; |
---|
24 | protected bool canBePushed; |
---|
25 | private bool isPushed; |
---|
26 | private float pushDir, pushPower; |
---|
27 | |
---|
28 | public Mob(Level level, Player owner) |
---|
29 | : base(level, owner) |
---|
30 | { |
---|
31 | currentOrder = new Order(); |
---|
32 | pathFinder = new AStar(level, this); |
---|
33 | walkingAnimation = new AnimationHelper(10, 2).Start(); |
---|
34 | immunityTime = 20; |
---|
35 | canBePushed = true; |
---|
36 | pushResistance = 0.0F; |
---|
37 | } |
---|
38 | |
---|
39 | protected float Direction { get { return dir; } } |
---|
40 | |
---|
41 | public AStar PathFinder { get { return pathFinder; } } |
---|
42 | |
---|
43 | //public override bool IsSolidTo(Entity ent) |
---|
44 | //{ |
---|
45 | // Mob mob = ent as Mob; |
---|
46 | // if (mob != null) |
---|
47 | // return mob.Owner != Owner; |
---|
48 | // return base.IsSolidTo(ent); |
---|
49 | //} |
---|
50 | |
---|
51 | public override void Update() |
---|
52 | { |
---|
53 | base.Update(); |
---|
54 | if (!(currentOrder is OrderIdle)) |
---|
55 | { |
---|
56 | currentOrder.Update(); |
---|
57 | if (currentOrder.Finished) |
---|
58 | { |
---|
59 | StopCurrentOrder(); |
---|
60 | } |
---|
61 | } |
---|
62 | |
---|
63 | if (isPushed) |
---|
64 | { |
---|
65 | MoveTo(dir, pushPower); |
---|
66 | isPushed = false; |
---|
67 | } |
---|
68 | } |
---|
69 | |
---|
70 | protected void StopCurrentOrder() |
---|
71 | { |
---|
72 | OnOrderFinished(); |
---|
73 | SetOrder(GetNextOrder()); |
---|
74 | } |
---|
75 | |
---|
76 | protected virtual void OnOrderFinished() |
---|
77 | { |
---|
78 | walkingAnimation.Reset(); |
---|
79 | } |
---|
80 | |
---|
81 | public void SetOrder(Order order) |
---|
82 | { |
---|
83 | currentOrder = order; |
---|
84 | order.Initialize(this); |
---|
85 | } |
---|
86 | |
---|
87 | public override void OnDamage(Unit attacker, int damage, float dir, float pushPower) |
---|
88 | { |
---|
89 | base.OnDamage(attacker, damage, dir, pushPower); |
---|
90 | if (canBePushed) |
---|
91 | Push(dir, pushPower); |
---|
92 | } |
---|
93 | |
---|
94 | private void Push(float dir, float pushPower) |
---|
95 | { |
---|
96 | isPushed = true; |
---|
97 | pushDir = dir; |
---|
98 | this.pushPower = pushPower - pushResistance; |
---|
99 | } |
---|
100 | |
---|
101 | private void MoveTo(float dir, float amount) |
---|
102 | { |
---|
103 | float moveX = (float)Math.Cos(dir) * amount; |
---|
104 | float moveZ = (float)Math.Sin(dir) * amount; |
---|
105 | |
---|
106 | Move(moveX, moveZ); |
---|
107 | } |
---|
108 | |
---|
109 | public bool MoveForward() |
---|
110 | { |
---|
111 | float moveX = (float)Math.Cos(dir) * moveSpeed; |
---|
112 | float moveZ = (float)Math.Sin(dir) * moveSpeed; |
---|
113 | |
---|
114 | if (Move(moveX, moveZ)) |
---|
115 | { |
---|
116 | walkingAnimation.UpdateStep(); |
---|
117 | return true; |
---|
118 | } |
---|
119 | |
---|
120 | return false; |
---|
121 | } |
---|
122 | |
---|
123 | public virtual Order GetNextOrder() |
---|
124 | { |
---|
125 | return new Order(); |
---|
126 | } |
---|
127 | |
---|
128 | public bool TurnTowards(float x, float z) |
---|
129 | { |
---|
130 | float angleTowards = (float)Math.Atan2(z - Z, x - X); |
---|
131 | |
---|
132 | dir = MathHelper.WrapAngle(dir); |
---|
133 | |
---|
134 | float angleDifferece = angleTowards - dir; |
---|
135 | angleDifferece = MathHelper.WrapAngle(angleDifferece); |
---|
136 | |
---|
137 | float maxTurn = 0.2F; |
---|
138 | float near = 1.0F; |
---|
139 | bool turnDone = angleDifferece * angleDifferece < near * near; |
---|
140 | if (angleDifferece < -maxTurn) angleDifferece = -maxTurn; |
---|
141 | if (angleDifferece > maxTurn) angleDifferece = maxTurn; |
---|
142 | dir += angleDifferece; |
---|
143 | dirID = GetDirectionID(); |
---|
144 | |
---|
145 | return turnDone; |
---|
146 | } |
---|
147 | |
---|
148 | protected void UpdateDir(float x, float z) |
---|
149 | { |
---|
150 | dir = (float)Math.Atan2(z - Z, x - X); |
---|
151 | dirID = GetDirectionID(); |
---|
152 | } |
---|
153 | |
---|
154 | public int GetDirectionID() |
---|
155 | { |
---|
156 | return (int)(Math.Floor(dir * 8 / MathHelper.TwoPi + 0.5F)) & 7; |
---|
157 | } |
---|
158 | } |
---|
159 | } |
---|