1 | using System; |
---|
2 | using System.Collections.Generic; |
---|
3 | using System.Linq; |
---|
4 | using System.Text; |
---|
5 | using CastleMaster.Ai.Orders; |
---|
6 | using CastleMaster.Graphics; |
---|
7 | using CastleMaster.Units.Mobs; |
---|
8 | |
---|
9 | namespace IsometricEngineTest.Ai.Orders |
---|
10 | { |
---|
11 | public class OrderMove : Order |
---|
12 | { |
---|
13 | private float x, z; |
---|
14 | private int ppi = 0; |
---|
15 | private bool excludeEndSolidness; |
---|
16 | private float stopDistance; |
---|
17 | |
---|
18 | public OrderMove(float x, float z, float stopDistance = 2.0F, bool excludeEndSolidness = false) |
---|
19 | { |
---|
20 | this.stopDistance = stopDistance; |
---|
21 | this.excludeEndSolidness = excludeEndSolidness; |
---|
22 | this.x = x; |
---|
23 | this.z = z; |
---|
24 | } |
---|
25 | |
---|
26 | public override Order Initialize(Mob mob) |
---|
27 | { |
---|
28 | base.Initialize(mob); |
---|
29 | mob.PathFinder.InitializePathFinder((int)(mob.X / Viewport.TILESIZE), (int)(mob.Z / Viewport.TILESIZE), (int)(x / Viewport.TILESIZE), (int)(z / Viewport.TILESIZE), excludeEndSolidness); |
---|
30 | return this; |
---|
31 | } |
---|
32 | |
---|
33 | public override void Update() |
---|
34 | { |
---|
35 | if (!mob.PathFinder.CanFindPath) |
---|
36 | return; |
---|
37 | if (mob.PathFinder.IsPathFinding) |
---|
38 | mob.PathFinder.FindPath(50); |
---|
39 | |
---|
40 | if (mob.PathFinder.Path.Count > 0 && ppi < mob.PathFinder.Path.Count) |
---|
41 | { |
---|
42 | Node currentTarget = mob.PathFinder.Path[ppi]; |
---|
43 | float xtPoint = currentTarget.X * Viewport.TILESIZE + 8.0F; |
---|
44 | float ztPoint = currentTarget.Z * Viewport.TILESIZE + 8.0F; |
---|
45 | |
---|
46 | if (mob.DistanceTo(xtPoint, ztPoint) > 8.0F) |
---|
47 | { |
---|
48 | if (mob.TurnTowards(xtPoint, ztPoint)) |
---|
49 | mob.MoveForward(); |
---|
50 | } |
---|
51 | else |
---|
52 | ppi++; |
---|
53 | } |
---|
54 | else |
---|
55 | { |
---|
56 | if (mob.DistanceTo(x, z) > 1.0F) |
---|
57 | { |
---|
58 | if (mob.TurnTowards(x, z)) |
---|
59 | mob.MoveForward(); |
---|
60 | } |
---|
61 | } |
---|
62 | } |
---|
63 | |
---|
64 | public override bool Finished |
---|
65 | { |
---|
66 | get |
---|
67 | { |
---|
68 | return (mob.DistanceTo(x, z) < stopDistance) || !mob.PathFinder.CanFindPath; |
---|
69 | } |
---|
70 | } |
---|
71 | } |
---|
72 | } |
---|