forked from eloipuertas/ES2015A
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathresources.diff
1257 lines (1152 loc) · 46.4 KB
/
resources.diff
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
diff --git a/March Death/Assets/Scripts/AI/AIController.cs b/March Death/Assets/Scripts/AI/AIController.cs
index 7751351..52335c6 100644
--- a/March Death/Assets/Scripts/AI/AIController.cs
+++ b/March Death/Assets/Scripts/AI/AIController.cs
@@ -160,9 +160,9 @@ namespace Assets.Scripts.AI
{
Storage.BuildingInfo i = Storage.Info.get.of(race, type);
- return (resources.getAmount(WorldResources.Type.FOOD) >= i.resources.food &&
- resources.getAmount(WorldResources.Type.WOOD) >= i.resources.wood &&
- resources.getAmount(WorldResources.Type.METAL) >= i.resources.metal);
+ return ResourcesPlacer.get(BasePlayer.ia).enoughResources(WorldResources.Type.FOOD, i.resources.food) &&
+ ResourcesPlacer.get(BasePlayer.ia).enoughResources(WorldResources.Type.WOOD, i.resources.wood) &&
+ ResourcesPlacer.get(BasePlayer.ia).enoughResources(WorldResources.Type.METAL, i.resources.metal);
}
public void CreateBuilding(BuildingTypes btype, Vector3 position, Quaternion rotation, AIArchitect architect)
diff --git a/March Death/Assets/Scripts/AI/AIDebugSystem.cs b/March Death/Assets/Scripts/AI/AIDebugSystem.cs
index a70731d..1ccc06e 100644
--- a/March Death/Assets/Scripts/AI/AIDebugSystem.cs
+++ b/March Death/Assets/Scripts/AI/AIDebugSystem.cs
@@ -115,13 +115,13 @@ public class AIDebugSystem : MonoBehaviour {
void showResources(int windowID)
{
GUI.contentColor = Color.red;
- GUI.Label(new Rect(marginLeft, 20, textWidth, textHeight), "Food: " + controller.resources.getAmount(WorldResources.Type.FOOD).ToString());
+ GUI.Label(new Rect(marginLeft, 20, textWidth, textHeight), "Food: " + ResourcesPlacer.get(BasePlayer.ia).Amount(WorldResources.Type.FOOD).ToString());
GUI.contentColor = Color.white;
- GUI.Label(new Rect(marginLeft, 40, textWidth, textHeight), "Wood: " + controller.resources.getAmount(WorldResources.Type.WOOD).ToString());
+ GUI.Label(new Rect(marginLeft, 40, textWidth, textHeight), "Wood: " + ResourcesPlacer.get(BasePlayer.ia).Amount(WorldResources.Type.WOOD).ToString());
GUI.contentColor = Color.yellow;
- GUI.Label(new Rect(marginLeft, 60, textWidth, textHeight), "Gold: " + controller.resources.getAmount(WorldResources.Type.GOLD).ToString());
+ GUI.Label(new Rect(marginLeft, 60, textWidth, textHeight), "Gold: " + ResourcesPlacer.get(BasePlayer.ia).Amount(WorldResources.Type.GOLD).ToString());
GUI.contentColor = Color.cyan;
- GUI.Label(new Rect(marginLeft, 80, textWidth, textHeight), "Metal: " + controller.resources.getAmount(WorldResources.Type.METAL).ToString());
+ GUI.Label(new Rect(marginLeft, 80, textWidth, textHeight), "Metal: " + ResourcesPlacer.get(BasePlayer.ia).Amount(WorldResources.Type.METAL).ToString());
GUI.DragWindow();
}
diff --git a/March Death/Assets/Scripts/Entities/Abilities/Create.cs b/March Death/Assets/Scripts/Entities/Abilities/Create.cs
index a02c284..cbafc89 100644
--- a/March Death/Assets/Scripts/Entities/Abilities/Create.cs
+++ b/March Death/Assets/Scripts/Entities/Abilities/Create.cs
@@ -23,9 +23,9 @@ public class Create : Ability
{
get
{
- return BasePlayer.getOwner(_entity).resources.IsEnough(WorldResources.Type.FOOD, _infoToBuild.resources.food) &&
- BasePlayer.getOwner(_entity).resources.IsEnough(WorldResources.Type.WOOD, _infoToBuild.resources.wood) &&
- BasePlayer.getOwner(_entity).resources.IsEnough(WorldResources.Type.METAL, _infoToBuild.resources.metal) &&
+ BasePlayer player = BasePlayer.getOwner(_entity);
+
+ return ResourcesPlacer.get(player).enoughResources(_info) &&
(_entity.status == EntityStatus.IDLE || _entity.status == EntityStatus.WORKING);
}
}
diff --git a/March Death/Assets/Scripts/Entities/Abilities/Sell.cs b/March Death/Assets/Scripts/Entities/Abilities/Sell.cs
index 532dcd4..43d3f2b 100644
--- a/March Death/Assets/Scripts/Entities/Abilities/Sell.cs
+++ b/March Death/Assets/Scripts/Entities/Abilities/Sell.cs
@@ -41,9 +41,10 @@ class Sell : Ability
if (resources != null)
{
- BasePlayer.getOwner(entity).resources.AddAmount(WorldResources.Type.WOOD, resources.wood);
- BasePlayer.getOwner(entity).resources.AddAmount(WorldResources.Type.METAL, resources.metal);
- BasePlayer.getOwner(entity).resources.AddAmount(WorldResources.Type.FOOD, resources.food);
+ BasePlayer player = BasePlayer.getOwner(entity);
+ ResourcesPlacer.get(player).Collect(WorldResources.Type.FOOD, resources.food);
+ ResourcesPlacer.get(player).Collect(WorldResources.Type.WOOD, resources.wood);
+ ResourcesPlacer.get(player).Collect(WorldResources.Type.METAL, resources.metal);
}
else
{
diff --git a/March Death/Assets/Scripts/Entities/Barrack.cs b/March Death/Assets/Scripts/Entities/Barrack.cs
index 1965ae6..683ab0e 100644
--- a/March Death/Assets/Scripts/Entities/Barrack.cs
+++ b/March Death/Assets/Scripts/Entities/Barrack.cs
@@ -8,8 +8,6 @@ public class Barrack : Building<Barrack.Actions>
{
public enum Actions {CREATED, DAMAGED, DESTROYED, CREATE_UNIT, BUILDING_FINISHED, HEALTH_UPDATED, ADDED_QUEUE};
- private IGameEntity _entity;
-
/// <summary>
/// Object initialization
/// </summary>
@@ -24,12 +22,5 @@ public class Barrack : Building<Barrack.Actions>
public override void Start()
{
base.Start();
-
- _entity = this.GetComponent<IGameEntity>();
-
- ResourcesEvents.get.registerResourceToEvents(_entity);
-
- if (BasePlayer.player.race.Equals(_info.race) && !type.Equals(BuildingTypes.STRONGHOLD))
- fire(Actions.CREATED, _entity);
}
}
diff --git a/March Death/Assets/Scripts/Entities/Building.cs b/March Death/Assets/Scripts/Entities/Building.cs
index a2df463..1e248fb 100644
--- a/March Death/Assets/Scripts/Entities/Building.cs
+++ b/March Death/Assets/Scripts/Entities/Building.cs
@@ -39,6 +39,7 @@ public abstract class Building<T> : GameEntity<T>, IBuilding where T : struct, I
}
/// Precach some actions
+ public T CREATED { get; set; }
public T DAMAGED { get; set; }
public T DESTROYED { get; set; }
public T CREATE_UNIT { get; set; }
@@ -106,9 +107,10 @@ public abstract class Building<T> : GameEntity<T>, IBuilding where T : struct, I
ConstructionGrid grid = gridGO.GetComponent<ConstructionGrid>();
Vector3 disc_pos = grid.discretizeMapCoords(gameObject.transform.position);
grid.liberatePosition(disc_pos);
- }
+ }
- base.OnDestroy();
+ ResourcesEvents.get.unregisterBuildingToEvents(this);
+ base.OnDestroy();
}
@@ -125,6 +127,7 @@ public abstract class Building<T> : GameEntity<T>, IBuilding where T : struct, I
/// </summary>
public override void Awake()
{
+ CREATED = (T)Enum.Parse(typeof(T), "CREATED", true);
DAMAGED = (T)Enum.Parse(typeof(T), "DAMAGED", true);
DESTROYED = (T)Enum.Parse(typeof(T), "DESTROYED", true);
CREATE_UNIT = (T)Enum.Parse(typeof(T), "CREATE_UNIT", true);
@@ -144,7 +147,6 @@ public abstract class Building<T> : GameEntity<T>, IBuilding where T : struct, I
// Setup base
base.Start();
-
_meetingPoint = getDefaultMeetingPoint();
activateFOWEntity();
@@ -157,6 +159,10 @@ public abstract class Building<T> : GameEntity<T>, IBuilding where T : struct, I
//return (info.buildingAttributes.wounds - _woundsReceived) * 100f / info.buildingAttributes.wounds;
// Set the status
setStatus(DefaultStatus);
+
+ // Register for AI and Player
+ ResourcesEvents.get.registerBuildingToEvents(this);
+ fire(CREATED, (IGameEntity)this);
}
/// <summary>
@@ -347,9 +353,10 @@ public abstract class Building<T> : GameEntity<T>, IBuilding where T : struct, I
UnitInfo unitInfo = Info.get.of(info.race, (UnitTypes)_creationQueue.Dequeue());
_creatingUnit = false;
- Player.getOwner(entity).resources.AddAmount(WorldResources.Type.WOOD, unitInfo.resources.wood);
- Player.getOwner(entity).resources.AddAmount(WorldResources.Type.METAL, unitInfo.resources.metal);
- Player.getOwner(entity).resources.AddAmount(WorldResources.Type.FOOD, unitInfo.resources.food);
+ BasePlayer player = BasePlayer.getOwner(entity);
+ ResourcesPlacer.get(player).Collect(WorldResources.Type.WOOD, unitInfo.resources.wood);
+ ResourcesPlacer.get(player).Collect(WorldResources.Type.METAL, unitInfo.resources.metal);
+ ResourcesPlacer.get(player).Collect(WorldResources.Type.FOOD, unitInfo.resources.food);
}
}
diff --git a/March Death/Assets/Scripts/Entities/GameEntity.cs b/March Death/Assets/Scripts/Entities/GameEntity.cs
index d7d9e37..a94afe3 100644
--- a/March Death/Assets/Scripts/Entities/GameEntity.cs
+++ b/March Death/Assets/Scripts/Entities/GameEntity.cs
@@ -480,13 +480,16 @@ public abstract class GameEntity<T> : Actor<T>, IGameEntity where T : struct, IC
});
}
- public void doIfUnit(Action<Unit> callIfTrue)
+ public bool doIfUnit(Action<Unit> callIfTrue)
{
Unit unit = this as Unit;
if (unit != null)
{
callIfTrue(unit);
+ return true;
}
+
+ return false;
}
public bool doIfBuilding(Action<IBuilding> callIfTrue)
diff --git a/March Death/Assets/Scripts/Entities/IGameEntity.cs b/March Death/Assets/Scripts/Entities/IGameEntity.cs
index dc31c37..6eeadae 100644
--- a/March Death/Assets/Scripts/Entities/IGameEntity.cs
+++ b/March Death/Assets/Scripts/Entities/IGameEntity.cs
@@ -43,7 +43,7 @@ public interface IGameEntity : IBaseActor
void receiveAttack(Unit from, bool isRanged);
- void doIfUnit(Action<Unit> callIfTrue);
+ bool doIfUnit(Action<Unit> callIfTrue);
bool doIfBuilding(Action<IBuilding> callIfTrue);
bool doIfResource(Action<Resource> callIfTrue);
bool doIfBarrack(Action<Barrack> callIfTrue);
diff --git a/March Death/Assets/Scripts/Entities/Resource.cs b/March Death/Assets/Scripts/Entities/Resource.cs
index 2620600..40e00c0 100644
--- a/March Death/Assets/Scripts/Entities/Resource.cs
+++ b/March Death/Assets/Scripts/Entities/Resource.cs
@@ -22,8 +22,6 @@ public class Resource : Building<Resource.Actions>
/// </summary>
Managers.SoundsManager sounds;
- public Statistics statistics;
-
// Constructor
public Resource() { }
@@ -193,8 +191,7 @@ public class Resource : Building<Resource.Actions>
private readonly object syncLock = new object();
bool hasCreatedCivil = false;
-
- private bool once = true;
+
List<GameObject> pendingProducers = new List<GameObject>();
List<GameObject> pendingWanderers = new List<GameObject>();
@@ -289,18 +286,15 @@ public class Resource : Building<Resource.Actions>
if (type.Equals(BuildingTypes.FARM))
{
- BasePlayer.getOwner(_entity).resources.AddAmount(WorldResources.Type.FOOD, amount);
- collectable.goods.type = Goods.GoodsType.FOOD;
+ collectable.goods.type = WorldResources.Type.FOOD;
}
else if (type.Equals(BuildingTypes.MINE))
{
- BasePlayer.getOwner(_entity).resources.AddAmount(WorldResources.Type.METAL, amount);
- collectable.goods.type = Goods.GoodsType.METAL;
+ collectable.goods.type = WorldResources.Type.METAL;
}
else
{
- BasePlayer.getOwner(_entity).resources.AddAmount(WorldResources.Type.WOOD, amount);
- collectable.goods.type = Goods.GoodsType.WOOD;
+ collectable.goods.type = WorldResources.Type.WOOD;
}
fire(Actions.COLLECTION, collectable);
}
@@ -313,7 +307,6 @@ public class Resource : Building<Resource.Actions>
/// <param name="type"></param>
protected override void createUnit(UnitTypes type)
{
-
if (harvestUnits < maxHarvestUnits)
{
_unitPosition.Set(_center.x, _center.y, _center.z);
@@ -330,11 +323,14 @@ public class Resource : Building<Resource.Actions>
setStatus(EntityStatus.WORKING);
_collectionRate += Info.get.of(race, UnitTypes.CIVIL).attributes.capacity;
+
+ fire(Actions.NEW_HARVEST, (IGameEntity)this);
}
else
{
base.createUnit(type);
}
+
_createStatus = createCivilStatus.IDLE;
}
/// <summary>
@@ -357,7 +353,6 @@ public class Resource : Building<Resource.Actions>
/// </summary>
public Unit recruitExplorer()
{
-
if (harvestUnits > 0)
{
Unit worker;
@@ -375,6 +370,8 @@ public class Resource : Building<Resource.Actions>
{
setStatus(EntityStatus.IDLE);
}
+
+ fire(Actions.NEW_EXPLORER, (IGameEntity)this);
return worker;
}
else
@@ -390,7 +387,6 @@ public class Resource : Building<Resource.Actions>
/// </summary>
private void recruitWorker(Unit explorer)
{
-
if (harvestUnits < maxHarvestUnits)
{
_collectionRate += explorer.info.attributes.capacity;
@@ -403,6 +399,8 @@ public class Resource : Building<Resource.Actions>
{
setStatus(EntityStatus.WORKING);
}
+
+ fire(Actions.NEW_HARVEST, (IGameEntity)this);
}
else
{
@@ -444,9 +442,6 @@ public class Resource : Building<Resource.Actions>
/// </summary>
public override void OnDestroy()
{
- statistics.getNegative();
- fire(Actions.DEL_STATS, statistics);
-
base.OnDestroy();
}
@@ -464,27 +459,7 @@ public class Resource : Building<Resource.Actions>
throw new Exception("That resource type does not exist!");
}
}
-
- private void SetupStatistics()
- {
- GameObject gameInformationObject = GameObject.Find("GameInformationObject");
- GameObject gameController = GameObject.Find("GameController");
- //ResourcesPlacer res_pl = gameController.GetComponent<ResourcesPlacer>();
-
- if (Player.isOfPlayer(_entity))
- {
- //FIXME bug after merging
- /*
- register(Actions.COLLECTION, res_pl.onCollection);
- register(Actions.CREATED, res_pl.onStatisticsUpdate);
- register(Actions.DEL_STATS, res_pl.onStatisticsUpdate);
- */
- }
-
- statistics = new Statistics(ResourceFromBuilding(type), (int)info.resourceAttributes.updateInterval, 10); // hardcoded, To modify, by now the collection rate is always 10, but theres no workers yet
- maxHarvestUnits = info.resourceAttributes.maxUnits;
- }
-
+
/// <summary>
/// Object initialization
/// </summary>
@@ -514,7 +489,7 @@ public class Resource : Building<Resource.Actions>
base.Start();
this.GetComponent<Rigidbody>().isKinematic = false;
- SetupStatistics();
+ maxHarvestUnits = info.resourceAttributes.maxUnits;
}
@@ -527,7 +502,6 @@ public class Resource : Building<Resource.Actions>
{
base.Update();
-
switch (status)
{
case EntityStatus.IDLE:
@@ -549,20 +523,6 @@ public class Resource : Building<Resource.Actions>
}
}
- public override void setStatus(EntityStatus newStatus)
- {
- if (status == EntityStatus.IDLE && newStatus == EntityStatus.WORKING)
- {
- if (once)
- {
- fire(Actions.CREATED, statistics);
- once = false;
- }
- }
-
- base.setStatus(newStatus);
- }
-
/// <summary>
/// When built, it's called
/// </summary>
diff --git a/March Death/Assets/Scripts/Entities/Unit.cs b/March Death/Assets/Scripts/Entities/Unit.cs
index d908855..c9705af 100644
--- a/March Death/Assets/Scripts/Entities/Unit.cs
+++ b/March Death/Assets/Scripts/Entities/Unit.cs
@@ -13,7 +13,7 @@ using Pathfinding;
/// </summary>
public class Unit : GameEntity<Unit.Actions>
{
- public enum Actions { CREATED, MOVEMENT_START, MOVEMENT_END, DAMAGED, EXTERMINATED, EAT, DIED, STAT_OUT, TARGET_TERMINATED, HEALTH_UPDATED };
+ public enum Actions { CREATED, MOVEMENT_START, MOVEMENT_END, DAMAGED, EXTERMINATED, EAT, DIED, TARGET_TERMINATED, HEALTH_UPDATED };
public enum Gender { MALE, FEMALE }
private EntityStatus _defaultStatus = EntityStatus.IDLE;
@@ -29,23 +29,19 @@ public class Unit : GameEntity<Unit.Actions>
}
}
- private IGameEntity _entity;
-
public Unit() { }
/// <summary>
/// Interval between resources update in miliseconds
/// </summary>
- const float RESOURCES_UPDATE_INTERVAL = 15.0f;
+ public const float RESOURCES_UPDATE_INTERVAL = 15.0f;
/// <summary>
/// Update follow distance when greater than this value
/// Do note this values is the SQUARED (^2) value of the real distance
/// </summary>
const float SQR_UPDATE_DISTANCE = 75.0f;
-
- Statistics statistics;
-
+
///<sumary>
/// Auto-unregister events when we are destroyed
///</sumary>
@@ -213,20 +209,14 @@ public class Unit : GameEntity<Unit.Actions>
/// </summary>
protected override void onFatalWounds()
{
- if(BasePlayer.player.race.Equals(_entity.info.race))
- fire(Actions.EXTERMINATED, _entity);
+ if(BasePlayer.player.race.Equals(info.race))
+ fire(Actions.EXTERMINATED, (IGameEntity)this);
- ResourcesEvents.get.unregisterUnitToEvents(_entity);
+ ResourcesEvents.get.unregisterUnitToEvents(this);
// Clear target, just in case
_target = null;
- if (BasePlayer.isOfPlayer(this))
- {
- statistics.growth_speed *= -1;
- fire(Actions.STAT_OUT, statistics);
- }
-
fire(Actions.DIED);
}
@@ -588,20 +578,9 @@ public class Unit : GameEntity<Unit.Actions>
{
activateFOWEntity();
}
-
- // Statistics available for both AI and Player
- GameObject gameInformationObject = GameObject.Find("GameInformationObject");
- GameObject gameController = GameObject.Find("GameController");
- //ResourcesPlacer res_pl = gameController.GetComponent<ResourcesPlacer>();
- //register(Actions.EAT, res_pl.onFoodConsumption);
-
- _entity = this.GetComponent<IGameEntity>();
-
- if (Player.getOwner(this).race.Equals(gameInformationObject.GetComponent<GameInformation>().GetPlayerRace()))
- {
- ResourcesEvents.get.registerUnitToEvents(_entity);
- }
+ // Register for both AI and player
+ ResourcesEvents.get.registerUnitToEvents(this);
// Set detour params (can't be done until Start is done)
if (!isImmobile)
@@ -611,8 +590,7 @@ public class Unit : GameEntity<Unit.Actions>
_detourAgent.UpdateParams();
}
- if(BasePlayer.player.race.Equals(_info.race))
- fire(Actions.CREATED, _entity);
+ fire(Actions.CREATED, (IGameEntity)this);
}
/// <summary>
@@ -645,19 +623,16 @@ public class Unit : GameEntity<Unit.Actions>
{
goldProduced = info.unitAttributes.goldProduction * resourcesElapsed;
goldConsumed = 0;
- }
-
+ }
+
// Update this unit resources
- BasePlayer.getOwner(this).resources.AddAmount(WorldResources.Type.GOLD, goldProduced);
- BasePlayer.getOwner(this).resources.SubstractAmount(WorldResources.Type.GOLD, goldConsumed);
- BasePlayer.getOwner(this).resources.SubstractAmount(WorldResources.Type.FOOD, foodConsumed);
-
- Goods goods = new Goods(); // Generate the goods the units eat
- goods.amount = 5;
- // goods.amount = this.info.unitAttributes.foodConsumption; // RAUL_UNCOMMENT
- goods.type = Goods.GoodsType.FOOD;
+ CollectableGood collectable = new CollectableGood(); // Generate the goods the units eat
+ collectable.entity = this;
+ collectable.goods = new Goods();
+ collectable.goods.type = WorldResources.Type.FOOD;
+ collectable.goods.amount = info.unitAttributes.foodConsumption;
- fire(Actions.EAT, goods);
+ fire(Actions.EAT, collectable);
}
// Status dependant functionality
diff --git a/March Death/Assets/Scripts/Events/ResourcesEvents.cs b/March Death/Assets/Scripts/Events/ResourcesEvents.cs
index 1a5d5fd..777e2ec 100644
--- a/March Death/Assets/Scripts/Events/ResourcesEvents.cs
+++ b/March Death/Assets/Scripts/Events/ResourcesEvents.cs
@@ -9,7 +9,7 @@ public class ResourcesEvents : Singleton<ResourcesEvents>
private ResourcesEvents() { }
// REGISTER METHODS
- public void registerResourceToEvents(IGameEntity entity)
+ public void registerBuildingToEvents(IGameEntity entity)
{
if (entity.info.isResource)
{
@@ -20,7 +20,7 @@ public class ResourcesEvents : Singleton<ResourcesEvents>
resource.register(Resource.Actions.CREATED, OnCreated);
resource.register(Resource.Actions.EXTERMINATED, OnDestroyed);
}
- if (entity.info.isBarrack)
+ else if (entity.info.isBarrack)
{
Barrack barrack = (Barrack)entity;
barrack.register(Barrack.Actions.CREATED, OnCreated);
@@ -38,7 +38,7 @@ public class ResourcesEvents : Singleton<ResourcesEvents>
}
}
- public void unregisterResourceToEvents(IGameEntity entity)
+ public void unregisterBuildingToEvents(IGameEntity entity)
{
if (entity.info.isResource)
{
@@ -49,7 +49,7 @@ public class ResourcesEvents : Singleton<ResourcesEvents>
resource.unregister(Resource.Actions.CREATED, OnCreated);
resource.unregister(Resource.Actions.EXTERMINATED, OnDestroyed);
}
- if (entity.info.isBarrack)
+ else if (entity.info.isBarrack)
{
Barrack barrack = (Barrack)entity;
barrack.unregister(Barrack.Actions.CREATED, OnCreated);
@@ -75,7 +75,7 @@ public class ResourcesEvents : Singleton<ResourcesEvents>
PopulationInfo.get.AddWorker();
IGameEntity entity = (IGameEntity)obj;
- ResourcesPlacer.get.StatisticsChanged(entity, CreatePackageFromEntity(entity));
+ ResourcesPlacer.get(BasePlayer.getOwner(entity)).StatisticsChanged(entity, CreatePackageFromEntity(entity));
}
@@ -84,21 +84,23 @@ public class ResourcesEvents : Singleton<ResourcesEvents>
PopulationInfo.get.RemoveWorker();
IGameEntity entity = (IGameEntity)obj;
- ResourcesPlacer.get.StatisticsChanged(entity, CreatePackageFromEntity(entity));
+ ResourcesPlacer.get(BasePlayer.getOwner(entity)).StatisticsChanged(entity, CreatePackageFromEntity(entity));
}
private void OnCollection(System.Object obj)
{
- Goods goods = (Goods) obj;
- ResourcesPlacer.get.Collect( (WorldResources.Type) goods.type , goods.amount);
+ CollectableGood collectable = (CollectableGood) obj;
+ BasePlayer player = BasePlayer.getOwner(collectable.entity);
+ ResourcesPlacer.get(player).Collect(collectable.goods.type, collectable.goods.amount);
}
private void OnConsumption(System.Object obj)
{
- Goods goods = (Goods)obj;
- ResourcesPlacer.get.Consume((WorldResources.Type) goods.type, goods.amount);
+ CollectableGood collectable = (CollectableGood)obj;
+ BasePlayer player = BasePlayer.getOwner(collectable.entity);
+ ResourcesPlacer.get(player).Collect(collectable.goods.type, collectable.goods.amount);
}
@@ -114,41 +116,52 @@ public class ResourcesEvents : Singleton<ResourcesEvents>
{ WorldResources.Type.METAL , res.metal }
};
- if (entity.info.isUnit) {
- if (((Unit)entity).type.Equals(UnitTypes.HERO))
+ BasePlayer owner = BasePlayer.getOwner(entity);
+ ResourcesPlacer placer = ResourcesPlacer.get(owner);
+
+ bool isUnit = entity.doIfUnit(unit =>
+ {
+ if (unit.type != UnitTypes.HERO)
{
- ResourcesPlacer.get.Buy(d);
+ placer.Buy(d);
}
- }
+ });
- if (entity.info.isBuilding)
+ if (!isUnit)
{
- ResourcesPlacer.get.Buy(d);
+ if (((BuildingInfo)entity.info).type != BuildingTypes.STRONGHOLD)
+ {
+ placer.Buy(d);
+ }
}
- ResourcesPlacer.get.updatePopulation();
+ placer.updatePopulation();
if (entity.info.isResource || entity.info.isUnit)
- ResourcesPlacer.get.StatisticsChanged(entity, CreatePackageFromEntity(entity));
+ {
+ placer.StatisticsChanged(entity, CreatePackageFromEntity(entity));
+ }
}
private void OnDestroyed(System.Object obj)
{
IGameEntity entity = (IGameEntity)obj;
+ BasePlayer owner = BasePlayer.getOwner(entity);
+ ResourcesPlacer placer = ResourcesPlacer.get(owner);
if (entity.info.isUnit)
{
Unit unit = (Unit)entity;
- ResourcesPlacer.get.RemoveEntity(WorldResources.Type.FOOD, entity);
+ placer.RemoveEntity(WorldResources.Type.FOOD, entity);
}
- if (entity.info.isResource)
+ else if (((BuildingInfo)entity.info).type != BuildingTypes.STRONGHOLD)
{
Resource resource = (Resource)entity;
- ResourcesPlacer.get.RemoveEntity(GetElementFromResource(resource), entity);
+ placer.RemoveEntity(GetElementFromResource(resource), entity);
}
- ResourcesPlacer.get.updatePopulation();
+ placer.updatePopulation();
}
@@ -174,8 +187,7 @@ public class ResourcesEvents : Singleton<ResourcesEvents>
if (entity.info.isUnit)
{
Unit unit = (Unit)entity;
- // packet = new GrowthStatsPacket(WorldResources.Type.FOOD, unit.info.unitAttributes.foodConsumption, 1f); // RAUL_UNCOMMENT (CHANGE SIGN¿?)
- packet = new GrowthStatsPacket( WorldResources.Type.FOOD , -5f , 10f );
+ packet = new GrowthStatsPacket(WorldResources.Type.FOOD, -unit.info.unitAttributes.foodConsumption, 1f);
}
return packet;
diff --git a/March Death/Assets/Scripts/Managers/BuildingsManager.cs b/March Death/Assets/Scripts/Managers/BuildingsManager.cs
index 00e3e92..16acb43 100644
--- a/March Death/Assets/Scripts/Managers/BuildingsManager.cs
+++ b/March Death/Assets/Scripts/Managers/BuildingsManager.cs
@@ -121,10 +121,7 @@ namespace Managers
{
Storage.BuildingInfo i = Storage.Info.get.of(race, type);
- IsEnoughFood = _player.resources.getAmount(WorldResources.Type.FOOD) >= i.resources.food;
- IsEnoughWood = _player.resources.getAmount(WorldResources.Type.WOOD) >= i.resources.wood;
- IsEnoughMetal = _player.resources.getAmount(WorldResources.Type.METAL) >= i.resources.metal;
- return IsEnoughFood && IsEnoughWood && IsEnoughMetal;
+ return ResourcesPlacer.get(BasePlayer.player).enoughResources(i.resources);
}
diff --git a/March Death/Assets/Scripts/Managers/IResourcesManager.cs b/March Death/Assets/Scripts/Managers/IResourcesManager.cs
index ff0d418..e69de29 100644
--- a/March Death/Assets/Scripts/Managers/IResourcesManager.cs
+++ b/March Death/Assets/Scripts/Managers/IResourcesManager.cs
@@ -1,58 +0,0 @@
-using WorldResources;
-namespace Managers
-{
-
- public interface IResourcesManager
- {
-
- /// <summary>
- /// Adds the amount
- /// </summary>
- /// <param name="type"></param>
- /// <param name="amount"></param>
- void AddAmount(Type type, float amount);
-
- /// <summary>
- /// Adds the amount
- /// </summary>
- /// <param name="other"></param>
- void AddAmount(WorldResources.Resource other);
-
- /// <summary>
- /// Substracts the amount and returns the remaining
- /// </summary>
- /// <param name="type"></param>
- /// <param name="amount"></param>
- /// <returns></returns>
- float SubstractAmount(Type type, float amount);
-
- /// <summary>
- /// Substracts the amount and returns the remaining
- /// </summary>
- /// <param name="other"></param>
- /// <returns></returns>
- float SubstractAmount(WorldResources.Resource other);
-
- /// <summary>
- /// Checks the current amount and returns true if is higher or equal to the input amount
- /// and false otherwise
- /// </summary>
- /// <param name="type"></param>
- /// <param name="amount"></param>
- /// <returns></returns>
- bool IsEnough(Type type, float amount);
-
- /// <summary>
- /// Checks the current amount and returns true if is higher or equal to the input amount
- /// and false otherwise
- /// </summary>
- /// <param name="type"></param>
- /// <param name="amount"></param>
- /// <returns></returns>
- bool IsEnough(WorldResources.Resource other);
-
-
- }
-
-
-}
\ No newline at end of file
diff --git a/March Death/Assets/Scripts/Managers/ResourcesManager.cs b/March Death/Assets/Scripts/Managers/ResourcesManager.cs
index fe6256a..e69de29 100644
--- a/March Death/Assets/Scripts/Managers/ResourcesManager.cs
+++ b/March Death/Assets/Scripts/Managers/ResourcesManager.cs
@@ -1,104 +0,0 @@
-using WorldResources;
-using System.Collections.Generic;
-
-
-namespace Managers
-{
-
- public class ResourcesManager : IResourcesManager
- {
-
-
- private Dictionary<Type, Deposit> _deposits;
-
- public ResourcesManager()
- {
- _deposits = new Dictionary<Type, Deposit>();
- }
-
-
- public void InitDeposit(WorldResources.Resource resource, int capacity = 0)
- {
- WorldResources.Type type = resource.GetResourceType();
- if (!_deposits.ContainsKey(type))
- {
- Deposit diposit = new Deposit(resource, capacity);
- _deposits.Add(type, diposit);
- }
- else
- {
- _deposits[type].AddAmount(capacity);
- }
- }
-
- public void EmptyDeposits()
- {
- foreach (var deposit in _deposits.Values)
- {
- deposit.Empty();
- }
- }
-
-
- public bool IsEnough(WorldResources.Resource other)
- {
- if (_deposits.ContainsKey(other.GetResourceType()))
- {
- return _deposits[other.GetResourceType()].GetAmount() >= other.GetAmount();
-
- }
- else
- throw new System.Exception("Type not in deposits");
-
- }
-
-
- public bool IsEnough(Type type, float amount)
- {
- return IsEnough(new WorldResources.Resource(type, amount));
- }
-
-
- public float SubstractAmount(WorldResources.Resource other)
- {
- if (_deposits.ContainsKey(other.GetResourceType()))
- {
- return _deposits[other.GetResourceType()].Substract(other);
- }
- else
- throw new System.Exception("Type not in deposits");
-
- }
-
-
- public float SubstractAmount(Type type, float amount)
- {
- return SubstractAmount(new WorldResources.Resource(type, amount));
-
- }
-
-
- public void AddAmount(WorldResources.Resource other)
- {
- if (_deposits.ContainsKey(other.GetResourceType()))
- {
- _deposits[other.GetResourceType()].AddAmount(other.GetAmount());
- }
- else
- throw new System.Exception("Type not in deposits");
- }
-
-
- public void AddAmount(Type type, float amount)
- {
- AddAmount(new WorldResources.Resource(type, amount));
- }
-
- public float getAmount(Type type)
- {
- return _deposits[type].GetAmount();
- }
-
- }
-
-}
diff --git a/March Death/Assets/Scripts/Player/BasePlayer.cs b/March Death/Assets/Scripts/Player/BasePlayer.cs
index b7ac22b..e0f02cc 100644
--- a/March Death/Assets/Scripts/Player/BasePlayer.cs
+++ b/March Death/Assets/Scripts/Player/BasePlayer.cs
@@ -11,13 +11,7 @@ public abstract class BasePlayer : Utils.SingletonMono<BasePlayer> {
/// </summary>
protected Storage.Races _selfRace;
public Storage.Races race { get { return _selfRace; } }
-
- /// <summary>
- /// The resources manager
- /// </summary>
- protected Managers.ResourcesManager _resources = new Managers.ResourcesManager();
- public Managers.ResourcesManager resources { get { return _resources; } }
-
+
/// <summary>
/// The buildings manager
/// </summary>
@@ -95,10 +89,7 @@ public abstract class BasePlayer : Utils.SingletonMono<BasePlayer> {
public void SetInitialResources(uint wood, uint food, uint metal, uint gold)
{
// TODO Consider adding a maximum capacity
- _resources.InitDeposit(new WorldResources.Resource(WorldResources.Type.FOOD, food));
- _resources.InitDeposit(new WorldResources.Resource(WorldResources.Type.WOOD, wood));
- _resources.InitDeposit(new WorldResources.Resource(WorldResources.Type.METAL, metal));
- _resources.InitDeposit(new WorldResources.Resource(WorldResources.Type.GOLD, gold));
+ ResourcesPlacer.get(this).InitializeResources(wood, food, metal, gold);
}
protected abstract void AddBuilding(IGameEntity entity);
diff --git a/March Death/Assets/Scripts/Player/Player.cs b/March Death/Assets/Scripts/Player/Player.cs
index 0230323..ffb3545 100644
--- a/March Death/Assets/Scripts/Player/Player.cs
+++ b/March Death/Assets/Scripts/Player/Player.cs
@@ -41,6 +41,8 @@ public class Player : BasePlayer
float timeToShow;
const float WAIT_FOR_FINISH = 3.5f;
+ ResourcesPlacer _resourcesPlacer;
+
// Use this for initialization
public override void Start()
{
@@ -60,7 +62,7 @@ public class Player : BasePlayer
// gameObject.AddComponent<ResourcesPlacer>();
missionStatus = new MissionStatus(playerId);
- ResourcesPlacer r = ResourcesPlacer.get; // initialization
+ _resourcesPlacer = ResourcesPlacer.get(this); // initialization
// TODO Set this values dynamically
minFoodTolerance = 100;
@@ -68,7 +70,7 @@ public class Player : BasePlayer
minMetalTolerance = 500;
minGoldTolerance = 500;
- foodDepleted = resources.getAmount(WorldResources.Type.FOOD) <= 0;
+ foodDepleted = _resourcesPlacer.Amount(WorldResources.Type.FOOD) <= 0;
ActorSelector selector = new ActorSelector()
{
@@ -196,7 +198,7 @@ public class Player : BasePlayer
private void displayResourceInfo(WorldResources.Type resourceType, int tolerance)
{
int amount;
- amount = Mathf.FloorToInt(resources.getAmount(resourceType));
+ amount = Mathf.FloorToInt(_resourcesPlacer.Amount(resourceType));
if (amount <= tolerance)
{
if (amount > 0)
@@ -212,7 +214,7 @@ public class Player : BasePlayer
if (!foodDepleted)
{
displayResourceInfo(WorldResources.Type.FOOD, minFoodTolerance);
- foodDepleted = resources.getAmount(WorldResources.Type.FOOD) <= 0;
+ foodDepleted = _resourcesPlacer.Amount(WorldResources.Type.FOOD) <= 0;
}
}
diff --git a/March Death/Assets/Scripts/Storage/Resources/Goods.cs b/March Death/Assets/Scripts/Storage/Resources/Goods.cs
index 2756b34..1bbe98c 100644
--- a/March Death/Assets/Scripts/Storage/Resources/Goods.cs
+++ b/March Death/Assets/Scripts/Storage/Resources/Goods.cs
@@ -13,13 +13,8 @@ namespace Storage
// constructor
public Goods() { }
- public enum GoodsType { FOOD, WOOD, METAL };
-
public float amount { get; set; }
- public GoodsType type { get; set; }
-
+ public WorldResources.Type type { get; set; }
}
}
-
-
diff --git a/March Death/Assets/Scripts/UI/ResourcesPlacer.cs b/March Death/Assets/Scripts/UI/ResourcesPlacer.cs
index 96c3b9b..d445c66 100644
--- a/March Death/Assets/Scripts/UI/ResourcesPlacer.cs
+++ b/March Death/Assets/Scripts/UI/ResourcesPlacer.cs
@@ -6,7 +6,7 @@ using System.Collections.Generic;
using Utils;
using System;
-public class ResourcesPlacer : Singleton<ResourcesPlacer>
+public class ResourcesPlacer
{
// attributes
private readonly string[] txt_names = { "meat", "wood", "metal" };
@@ -18,7 +18,7 @@ public class ResourcesPlacer : Singleton<ResourcesPlacer>
/// Population text where number of units are displayed
/// </summary>
private Text pop;
- private Player _player;
+ private BasePlayer _owner;
private Sprite up;
private Sprite down;
@@ -28,34 +28,46 @@ public class ResourcesPlacer : Singleton<ResourcesPlacer>
Dictionary<WorldResources.Type, int> resources;
Dictionary<WorldResources.Type, Dictionary<IGameEntity, GrowthStatsPacket>> statistics;
+ private static Dictionary<BasePlayer, ResourcesPlacer> _instances = new Dictionary<BasePlayer, ResourcesPlacer>();
- private ResourcesPlacer()
+ private ResourcesPlacer(BasePlayer owner)
{
+ _owner = owner;
res_amounts = new List<Text>();
res_stats = new List<Text>();
arrows = new List<Image>();
Setup();
+ }
- initializeResources();
- initializeStatistics();
-
- updateAmounts();
- updateStatistics();
- updatePopulation();
+ public static ResourcesPlacer get(BasePlayer player)
+ {
+ if (!_instances.ContainsKey(player))
+ {
+ _instances.Add(player, new ResourcesPlacer(player));
+ }
+
+ return _instances[player];
}