1 | using System; |
---|
2 | using System.Collections.Generic; |
---|
3 | using System.Linq; |
---|
4 | using System.Text; |
---|
5 | using CastleMaster.World; |
---|
6 | using CastleMaster.Players; |
---|
7 | using CastleMaster.Graphics; |
---|
8 | using IsometricEngineTest.Ai.Orders; |
---|
9 | using Microsoft.Xna.Framework; |
---|
10 | using CastleMaster.Entities.TileEntities; |
---|
11 | using CastleMaster.Ai.Orders; |
---|
12 | |
---|
13 | namespace CastleMaster.Units.Mobs |
---|
14 | { |
---|
15 | public class MobWoodcutter : Mob |
---|
16 | { |
---|
17 | private enum OrderType { CHOP, MOVE, AVOID, NONE } |
---|
18 | |
---|
19 | private TileEntityTree currentTarget; |
---|
20 | private bool isChopping = false; |
---|
21 | private AnimationHelper choppingAnimation; |
---|
22 | private int currentSpriteX; |
---|
23 | private OrderType currentOrderType = OrderType.NONE; |
---|
24 | private int lastSameOrderAmount = 0; |
---|
25 | private bool engangeChopping = false; |
---|
26 | |
---|
27 | public MobWoodcutter(Level level, Player owner) |
---|
28 | : base(level, owner) |
---|
29 | { |
---|
30 | HasHealth = true; |
---|
31 | maxHealth = 20; |
---|
32 | |
---|
33 | spriteSize.X = 17; |
---|
34 | spriteSize.Y = 20; |
---|
35 | screenRectOffset.Update(8, 12, 8 + spriteSize.X, 12 + spriteSize.Y); |
---|
36 | renderOffset.Y = 20; |
---|
37 | |
---|
38 | highlightOffset.X = 11; |
---|
39 | |
---|
40 | rectOffset.Update(-4.0F, -4.0F, 5.0F, 5.0F); |
---|
41 | isSolid = true; |
---|
42 | |
---|
43 | moveSpeed = 2.0F; |
---|
44 | choppingAnimation = new AnimationHelper(10, 5, false, 3); |
---|
45 | choppingAnimation.RoundEnded += delegate() |
---|
46 | { |
---|
47 | currentTarget.AvaliableLogs--; |
---|
48 | if (currentTarget.AvaliableLogs <= 0) |
---|
49 | { |
---|
50 | StopChopping(); |
---|
51 | currentTarget.Remove(); |
---|
52 | currentTarget = null; |
---|
53 | } |
---|
54 | }; |
---|
55 | } |
---|
56 | |
---|
57 | public override int TypeID |
---|
58 | { |
---|
59 | get { return 0; } |
---|
60 | } |
---|
61 | |
---|
62 | private void StartChopping() |
---|
63 | { |
---|
64 | choppingAnimation.Start(); |
---|
65 | isChopping = true; |
---|
66 | } |
---|
67 | |
---|
68 | private void StopChopping() |
---|
69 | { |
---|
70 | isChopping = false; |
---|
71 | currentOrderType = OrderType.NONE; |
---|
72 | choppingAnimation.Stop(); |
---|
73 | choppingAnimation.Reset(); |
---|
74 | } |
---|
75 | |
---|
76 | public override void OnFunctionClick(float x, float z, int sameOrderAmount, bool wasCalledBefore) |
---|
77 | { |
---|
78 | TileEntity te = level.GetTileEntity(this, (int)(x / Viewport.TILESIZE), (int)(z / Viewport.TILESIZE)); |
---|
79 | if (te != null && typeof(TileEntityTree).IsAssignableFrom(te.GetType())) |
---|
80 | { |
---|
81 | if (currentTarget != null) StopChopping(); |
---|
82 | currentOrderType = OrderType.CHOP; |
---|
83 | currentTarget = (TileEntityTree)te; |
---|
84 | SetOrder(new OrderMove(currentTarget.X, currentTarget.Z, 20.0F, sameOrderAmount * 4.0F, true, !wasCalledBefore)); |
---|
85 | } |
---|
86 | else |
---|
87 | { |
---|
88 | if (isChopping) |
---|
89 | { |
---|
90 | StopChopping(); |
---|
91 | currentTarget = null; |
---|
92 | } |
---|
93 | SetOrder(new OrderMove(x, z, createNewPathFinder: !wasCalledBefore, stopOnMoveFail: true)); |
---|
94 | } |
---|
95 | lastSameOrderAmount = sameOrderAmount; |
---|
96 | } |
---|
97 | |
---|
98 | protected override void OnOrderFinished() |
---|
99 | { |
---|
100 | base.OnOrderFinished(); |
---|
101 | |
---|
102 | if (currentOrderType == OrderType.CHOP) |
---|
103 | StartChopping(); |
---|
104 | } |
---|
105 | |
---|
106 | public override void Update() |
---|
107 | { |
---|
108 | base.Update(); |
---|
109 | |
---|
110 | if (isChopping) |
---|
111 | { |
---|
112 | TurnTowards(currentTarget.X, currentTarget.Z); |
---|
113 | choppingAnimation.UpdateStep(); |
---|
114 | } |
---|
115 | else if (!isChopping && currentOrderType == OrderType.CHOP) |
---|
116 | { |
---|
117 | if (currentOrder != null && currentOrder is OrderMove && ((OrderMove)currentOrder).FailingToMove) |
---|
118 | { |
---|
119 | StopChopping(); |
---|
120 | StopCurrentOrder(); |
---|
121 | } |
---|
122 | } |
---|
123 | |
---|
124 | |
---|
125 | currentSpriteX = isChopping ? choppingAnimation.CurrentFrame : walkingAnimation.CurrentFrame; |
---|
126 | } |
---|
127 | |
---|
128 | public override void Render(RenderHelper renderer) |
---|
129 | { |
---|
130 | base.Render(renderer); |
---|
131 | renderer.Render(ScreenPos, currentSpriteX, dirID, Resources.SPRITESHEET_WOODCUTTER, colorizer, Viewport.ZOOM); |
---|
132 | } |
---|
133 | } |
---|
134 | } |
---|