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 | |
---|
13 | namespace CastleMaster.Units.Mobs |
---|
14 | { |
---|
15 | public class Mob : Unit |
---|
16 | { |
---|
17 | private Order currentOrder; |
---|
18 | private float dir = 0.0F; |
---|
19 | private AStar pathFinder; |
---|
20 | protected int dirID; |
---|
21 | protected AnimationHelper walkingAnimation; |
---|
22 | |
---|
23 | public Mob(Level level, Player owner) |
---|
24 | : base(level, owner) |
---|
25 | { |
---|
26 | currentOrder = new Order(); |
---|
27 | pathFinder = new AStar(level, this); |
---|
28 | walkingAnimation = new AnimationHelper(10, 2).Start(); |
---|
29 | } |
---|
30 | |
---|
31 | public AStar PathFinder { get { return pathFinder; } } |
---|
32 | |
---|
33 | public override void Update() |
---|
34 | { |
---|
35 | base.Update(); |
---|
36 | if (!(currentOrder is OrderIdle)) |
---|
37 | { |
---|
38 | currentOrder.Update(); |
---|
39 | if (currentOrder.Finished) |
---|
40 | { |
---|
41 | StopCurrentOrder(); |
---|
42 | } |
---|
43 | } |
---|
44 | } |
---|
45 | |
---|
46 | protected void StopCurrentOrder() |
---|
47 | { |
---|
48 | OnOrderFinished(); |
---|
49 | SetOrder(GetNextOrder()); |
---|
50 | } |
---|
51 | |
---|
52 | protected virtual void OnOrderFinished() |
---|
53 | { |
---|
54 | walkingAnimation.Reset(); |
---|
55 | } |
---|
56 | |
---|
57 | public void SetOrder(Order order) |
---|
58 | { |
---|
59 | currentOrder = order; |
---|
60 | order.Initialize(this); |
---|
61 | } |
---|
62 | |
---|
63 | public override void OnFunctionClick() |
---|
64 | { |
---|
65 | } |
---|
66 | |
---|
67 | public bool MoveForward() |
---|
68 | { |
---|
69 | float moveX = (float)Math.Cos(dir) * moveSpeed; |
---|
70 | float moveZ = (float)Math.Sin(dir) * moveSpeed; |
---|
71 | |
---|
72 | if (Move(moveX, moveZ)) |
---|
73 | { |
---|
74 | walkingAnimation.UpdateStep(); |
---|
75 | return true; |
---|
76 | } |
---|
77 | |
---|
78 | return false; |
---|
79 | } |
---|
80 | |
---|
81 | public virtual Order GetNextOrder() |
---|
82 | { |
---|
83 | return new Order(); |
---|
84 | } |
---|
85 | |
---|
86 | public bool TurnTowards(float x, float z) |
---|
87 | { |
---|
88 | float angleTowards = (float)Math.Atan2(z - Z, x - X); |
---|
89 | |
---|
90 | dir = MathHelper.WrapAngle(dir); |
---|
91 | |
---|
92 | float angleDifferece = angleTowards - dir; |
---|
93 | angleDifferece = MathHelper.WrapAngle(angleDifferece); |
---|
94 | |
---|
95 | float maxTurn = 0.2F; |
---|
96 | float near = 1.0F; |
---|
97 | bool turnDone = angleDifferece * angleDifferece < near * near; |
---|
98 | if (angleDifferece < -maxTurn) angleDifferece = -maxTurn; |
---|
99 | if (angleDifferece > maxTurn) angleDifferece = maxTurn; |
---|
100 | dir += angleDifferece; |
---|
101 | dirID = GetDirectionID(); |
---|
102 | |
---|
103 | return turnDone; |
---|
104 | } |
---|
105 | |
---|
106 | public int GetDirectionID() |
---|
107 | { |
---|
108 | return (int)(Math.Floor(dir * 8 / MathHelper.TwoPi + 0.5F)) & 7; |
---|
109 | } |
---|
110 | } |
---|
111 | } |
---|