-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathWorld.py
152 lines (127 loc) · 5.7 KB
/
World.py
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
from Map import Map
from Events.EventManager import EventManager
from TankManagement.TankManager import TankManager
from TankSystems.TankMovementSystem import TankMovementSystem
from TankSystems.TankShootingSystem import TankShootingSystem
from TankSystems.TankHealthSystem import TankHealthSystem
from TankSystems.DisplaySystem import DisplaySystem
from TankSystems.TankRespawnSystem import TankRespawnSystem
from TankSystems.PositionBonusSystem import PositionBonusSystem
from TankSystems.BaseCaptureSystem import BaseCaptureSystem
import inspect
import Events.Events as AllEvents
from Aliases import jsonDict, positionTuple
from Bot import Bot
from Entities.EntityManagementSystem import EntityManagementSystem
class World:
def __init__(self, map: jsonDict, gameState: jsonDict, playerId: int) -> None:
"""
Initializes the game world.
:param map: A dictionary containing the map data.
:param gameState: A dictionary containing the game state data.
"""
self.__playerId = playerId
self.__map = Map(map)
self.__initializeEventManager()
self.__tankManager = TankManager(self.__eventManager)
self.__initializeSystems(gameState)
self.__bot = Bot(self.__map, self.__eventManager, self.__movementSystem, self.__shootingSystem,
self.__entityManagementSystem)
def __initializeEventManager(self) -> None:
"""
Initializes the event manager.
"""
self.__eventManager = EventManager()
# init all events
allEvents = inspect.getmembers(AllEvents, inspect.isclass)
for _, cls in allEvents:
self.__eventManager.registerEvent(cls)
def __initializeSystems(self, gameState: jsonDict) -> None:
"""
Initializes the various systems used in the game world.
:param gameState: A dictionary containing the game state data.
"""
self.__movementSystem = TankMovementSystem(self.__map, self.__eventManager)
self.__displaySystem = DisplaySystem(self.__map, self.__eventManager)
self.__shootingSystem = TankShootingSystem(self.__map, self.__eventManager, gameState["attack_matrix"],
gameState["catapult_usage"])
self.__healthSystem = TankHealthSystem(self.__eventManager)
self.__respawnSystem = TankRespawnSystem(self.__eventManager)
self.__positionBonusSystem = PositionBonusSystem(self.__map, self.__eventManager)
self.__baseCaptureSystem = BaseCaptureSystem(self.__map, self.__eventManager)
self.__entityManagementSystem = EntityManagementSystem(gameState, self.__playerId)
def resetSystems(self, gameState: jsonDict) -> None:
"""
Resets the various systems used in the game world to their initial state.
:param gameState: A dictionary containing the game state data.
"""
self.__tankManager.reset()
self.__displaySystem.reset()
self.__movementSystem.reset()
self.__shootingSystem.reset(gameState["attack_matrix"], gameState["catapult_usage"])
self.__healthSystem.reset()
self.__respawnSystem.reset()
self.__positionBonusSystem.reset()
self.__baseCaptureSystem.reset()
self.__bot.reset()
self.__entityManagementSystem.reset()
def getEntityManagementSystem(self):
return self.__entityManagementSystem
def addMissingPlayers(self, gameState: jsonDict) -> None:
"""
Adds tanks that are in the game state but not in the local world.
:param gameState: A dictionary containing the game state data.
"""
self.__entityManagementSystem.addMissingEntities(gameState)
def addMissingTanks(self, gameState: jsonDict) -> None:
"""
Adds tanks that are in the game state but not in the local world.
:param gameState: A dictionary containing the game state data.
"""
for tankId, tankData in gameState["vehicles"].items():
tankId = str(tankId)
if not self.__tankManager.hasTank(tankId):
self.__tankManager.addTank(tankId, tankData)
def shoot(self, tankId: str, targetPosition: positionTuple) -> None:
"""
Shoots at a target position.
:param tankId: The ID of the tank doing the shooting.
:param targetPosition: The position being targeted.
"""
self.__shootingSystem.shoot(tankId, targetPosition)
def move(self, tankId: str, targetPosition: positionTuple) -> None:
"""
Moves a tank to a target position.
:param tankId: The ID of the tank being moved.
:param targetPosition: The position being moved to.
"""
self.__movementSystem.move(tankId, targetPosition)
def getBot(self) -> Bot:
"""
Gets the game's bot.
:return: The game's bot.
"""
return self.__bot
def turn(self, gameState: jsonDict) -> None:
"""
Performs the turn logic for the game world.
:param gameState: currentGame state at the end of the turn.
"""
currentPlayer = gameState["current_player_idx"]
self.__respawnSystem.turn()
self.__positionBonusSystem.turn()
self.__baseCaptureSystem.turn()
self.__displaySystem.turn()
self.__shootingSystem.turn(currentPlayer)
self.__entityManagementSystem.turn(gameState)
def round(self) -> None:
"""
Performs the round logic for the game world.
:param currentPlayer: ID of the player whose turn it is.
"""
self.__baseCaptureSystem.round()
def quit(self):
"""
Quits the game by closing the display.
"""
self.__displaySystem.quit()