def init(self, title, graphicsFile): """Given the graphics element of the configuration file, parses the properties therein and sets them as properties of the View class.""" XMLroot = ElementTree.parse(graphicsFile).getroot() for configNode in XMLroot: setattr(self, configNode.tag, Util.convert(configNode.text)) Input.init(self) TextManager.init() # Creates screen self._window = sfml.RenderWindow(sfml.VideoMode(self.WINDOW_WIDTH, self.WINDOW_HEIGHT), title) self._pane = NGUIPane(0, 0, self.WINDOW_WIDTH, self.WINDOW_HEIGHT) self._pane.name = "HumanView" self.mouseFocus = None
def onMouseUpEvent(self, dispatcher, event): if event.button != Mouse.LEFT: return False if not self.clicked: return False self.clicked = False if self.dragRect != Rectangle(): # Dragging units = self.world.getUnitsWithinRectangle(self.dragRect, self.faction) if not Input.isKeyPressed(Keyboard.L_SHIFT) and not Input.isKeyPressed(Keyboard.L_ALT): self.SUController.deselectAll() if Input.isKeyPressed(Keyboard.L_ALT): self.SUController.removeUnits(units) else: self.SUController.addUnits(units) self.dragRect = Rectangle() return True # If the mouse moved too much, don't do anything if Util.vectorToDistance(event.position - self.clickedPos) > SelectionController.SELECT_DISTANCE: return False # If clicked is true and a mouseUp event was caught here, it means we # we haven't stacked another controller on top of this one. So it's safe # to pick a single entity. worldPos = self.gameView.viewToWorld(event.position) unit = self.world.getClosestUnit(worldPos, self.faction) dist = Util.vectorToDistance(unit.getComponent(PositionComponent).p - worldPos) # Stop if closest unit was too far away from mouse if self.gameView.scaleWorldToView(Vector2(dist, 0)).x > SelectionController.SELECT_DISTANCE: if not Input.isKeyPressed(Keyboard.L_SHIFT): self.SUController.deselectAll() return True # Definitely changing selection if not Input.isKeyPressed(Keyboard.L_SHIFT) and not Input.isKeyPressed(Keyboard.L_ALT): self.SUController.deselectAll() if self.doubleClickClock.elapsed_time.milliseconds > SelectionController.DOUBLE_CLICK_INTERVAL: # SINGLE CLICK if Input.isKeyPressed(Keyboard.L_ALT): self.SUController.removeUnit(unit) else: self.SUController.addUnit(unit) else: # DOUBLE CLICK rect = self.gameView.camera.viewRect units = self.world.getUnitsWithinRectangle(rect, self.faction) units = [u for u in units if u.name == unit.name] if Input.isKeyPressed(Keyboard.L_ALT): self.SUController.removeUnits(units) else: self.SUController.addUnits(units) self.doubleClickClock.restart() return True
def run(self, dt): Input.processInput() self._humanView.draw()
def initialise(self): # Register event types Event.registerEventType("ENTITY_ADDED") Event.registerEventType("ENTITY_REMOVED") Event.registerEventType("ENTITY_MOVING") Event.registerEventType("ENTITY_STOPPED") Event.registerEventType("ENTITY_MOVED") Event.registerEventType("ENTITY_FACING_UPDATED") Event.registerEventType("ENTITY_ATTACKING") Event.registerEventType("ENTITY_DIED") Event.registerEventType("RESEARCH_DEPLETED") Event.registerEventType("ENTITY_HARVEST_START") Event.registerEventType("ENTITY_HARVEST_STOP") Event.registerEventType("ENTITY_RESEARCH_DUMPED") # Load stuff ResourceManager.loadAnimations("planet5521/data/animations.xml") EntityFactory.getSingleton().readFile("planet5521/data/entities.xml") EntityViewFactory.readFile("planet5521/data/views.xml") self._world = Planet5521World(200, 80) self._gameView = Planet5521GameView(0, 0, 1280, 720, self._world) self._gameView.name = "Game View" self._world._em.registerListener(Event.ENTITY_ADDED, self._gameView.entityCreated) # Entity destruction is handled at the entity EventManager level # Make environment self._world.produce("Sky", (0, 0)) self._world.produce("Ground", (0, self._world.height - 10)) self._world.produce("Ground Surface", (0, self._world.height - 10)) for _ in range(0): e = self._world.produce("Cloud", Vector2(Rand.r.random()*self._world.width, Rand.r.random()*(self._world.height - 20))) e.getComponent(VelocityComponent).v.x = Rand.r.random() # Make some units insectFaction = self._world.factions["Insects"] for _ in range(15): #for _ in range(0): insectFaction.produce("Insect Warrior", Vector2(10 + Rand.r.random()*20, self._world.height - 10-1.4)) for _ in range(15): #for _ in range(0): insectFaction.produce("Insect Warrior", Vector2(170 + Rand.r.random()*20, self._world.height - 10-1.4)) terrans = self._world.factions["Terrans"] for _ in range(15): #for _ in range(0): entity = terrans.produce("Terran Grunt", Vector2(80 + (Rand.r.random())*10, self._world.height - 10-1)) entity.getComponent(FacingComponent).facingLeft = Rand.r.random() < 0.5 for _ in range(15): #for _ in range(0): entity = terrans.produce("Terran Grunt", Vector2(110 + (Rand.r.random())*10, self._world.height - 10-1)) entity.getComponent(FacingComponent).facingLeft = Rand.r.random() < 0.5 for _ in range(15): #for _ in range(0): entity = terrans.produce("Terran Engineer", Vector2(90 + (Rand.r.random())*20, self._world.height - 10-1)) entity.getComponent(FacingComponent).facingLeft = Rand.r.random() < 0.5 #insectFaction.produce("Insect Warrior", Vector2(100, self._world.height - 10-1.4)) terrans.produce("Terran Barracks", Vector2(100,self._world.height - 10)) # Controllers self._gameView.addListener(TestGroundTroopController(self._world, self._gameView)) selectionController = SelectedUnitsController(self._world, self._gameView) self._gameView.addListener(selectionController) self._gameView.addListener(SelectionController(self._world, self._gameView, terrans, selectionController)) self._gameView.addListener(CameraController(self._world, self._gameView)) # Close by interactions #terrans.produce("Terran Grunt", Vector2(80, self._world.height - 10-1)) #terrans.produce("Terran Grunt", Vector2(80.5, self._world.height - 10-1)) #entity = terrans.produce("Terran Engineer", Vector2(81, self._world.height - 10-1)) #insectFaction.produce("Insect Warrior", Vector2(65, self._world.height - 10-1.4)) # Dropship is going to have something different. #dp = self._world.produce("Dropship", Vector2(10, 10)) #self._gameView.addListener(DropshipController(dp)) self._humanView.getPane().addChild(self._gameView) # Make humanview visible self._pm.processList.append(ViewUpdateProcess(self._gameView)) self._pm.processList.append(WorldUpdateProcess(self._world)) self._pm.processList.append(EntityFadeAndRemovalProcess(self._world, self._gameView)) self._pm.processList.append(HumanViewProcess(self._humanView)) #self._pm.processList.append(CinematicCamProcess(self._gameView.camera)) # TODO: This should perhaps be in Planet5521GameView? # TODO: And instead of changing the position, it should change view's dy self.bdP = BuildingDestructionProcess(self._world, self._gameView) self._pm.processList.append(self.bdP) self._world._em.registerListener(Event.ENTITY_DIED, self.entityDied) # For checking window resize events Input.addListener(self)