def create_player(): global player player = EntityManager.Instance().create_entity('@', z=10) EntityManager.Instance().add_component(player, Transform2D(Vector2D(10, 10))) EntityManager.Instance().add_component(player, Health()) EntityManager.Instance().add_component( player, Collider(COLLIDER_PLAYER, COLLIDER_PLAYER | COLLIDER_WALL)) GameManager.Instance().message( "Bryant entered the strange room hesitantly.", Colors.red)
def update(self, dt): # Update all projectiles for e, component in EntityManager.Instance().pairs_for_type(Projectile): currentPos = e.components["Transform2D"].position EventManager.Instance().fireEvent("EVENT_MoveEntity", {"entity" : e, "vector2D" : Direction.getVector(component.direction)}) # Remove all removable entities entities = EntityManager.Instance().list_entities() for entity in entities: if entity.removable: EntityManager.Instance().remove_entity(entity)
def loadMap(self, mapArr): for row in range(0, len(mapArr)): for col in range(0, len(mapArr[0])): entity = EntityManager.Instance().create_entity( mapArr[row][col]) EntityManager.Instance().add_component( entity, Transform2D(Vector2D(col, row))) if entity.symbol == "#": EntityManager.Instance().add_component( entity, Collider(COLLIDER_WALL, COLLIDER_PLAYER | COLLIDER_WALL))
def spawnProjectile(self, args): origin = args["origin"] direction = args["direction"] damage = args["damage"] p = EntityManager.Instance().create_entity("+", z=9) EntityManager.Instance().add_component(p, Transform2D(origin)) EntityManager.Instance().add_component(p, Projectile(damage, direction)) EntityManager.Instance().add_component( p, Collider(COLLIDER_PROJECTILE, COLLIDER_PLAYER | COLLIDER_WALL)) GameManager.Instance().message("A bullet shot rings through the air!", Colors.yellow)
def add_robot(self, type, x, y): # Create a bot and link back the entity so that user can reference other components bot = type() robot = EntityManager.Instance().create_entity(bot.symbol, z=10) bot.action = Robot(robot, bot) EntityManager.Instance().add_component(robot, Transform2D(Vector2D(x, y))) EntityManager.Instance().add_component(robot, Health()) EntityManager.Instance().add_component(robot, Collider(COLLIDER_PLAYER, COLLIDER_PLAYER | COLLIDER_WALL)) EntityManager.Instance().add_component(robot, bot.action) GameManager.Instance().message("Bryant entered the strange room hesitantly.", Colors.red) EventManager.Instance().fireEvent("EVENT_StatsUpdated", [{"HP: {0}/{1}".format(robot.components["Health"].health, robot.components["Health"].maxHealth) : {"color" : Colors.gold}}, {"MP: 5/20" : {"color" : Colors.gold}}]) return robot
def attemptMove(self, args): entity = args["entity"] transform = EntityManager.Instance().component_for_entity(entity, Transform2D) collider = EntityManager.Instance().component_for_entity(entity, Collider) if transform != None: vector2D = args["vector2D"] newPosition = transform.position + vector2D # Check for collisions for e, component in EntityManager.Instance().pairs_for_type(Collider): if e != entity and newPosition == e.components["Transform2D"].position and collider != None: if collider.collidesWithMask & component.mask: self.collision(entity, e) return # No collisions, so it is okay to move! transform.position = newPosition
def update(self, dt): # Iterate over every robot for e, robot in EntityManager.Instance().pairs_for_type(Robot): if robot._ttr <= 0: robot.bot.run(self.getCurrentGameStateForRobot(robot)) robot._emit() else: robot._ttr -= 1
def Init(self): # Setup links between the various managers SystemManager.Instance().Init() EntityManager.Instance().Init() EventManager.Instance().Init() self.lastUpdated = 0 self.running = True self.message_log = []
def loadMap(self, mapArr): self.map_size_x = len(mapArr[0]) self.map_size_y = len(mapArr) print("Map Size: ({0}, {1})".format(self.map_size_x, self.map_size_y)) for row in range(0, len(mapArr)): for col in range(0, len(mapArr[0])): if mapArr[row][col] in list(healthLookupTable.keys()): entity = EntityManager.Instance().create_entity( mapArr[row][col]) EntityManager.Instance().add_component( entity, Transform2D(Vector2D(col, row))) EntityManager.Instance().add_component( entity, Health(healthLookupTable[mapArr[row][col]])) EntityManager.Instance().add_component( entity, Collider( COLLIDER_WALL, COLLIDER_PLAYER | COLLIDER_WALL | COLLIDER_PROJECTILE))
def Init(self, tps=100): # Setup links between the various managers SystemManager.Instance().Init() EntityManager.Instance().Init() EventManager.Instance().Init() self.tps = tps self.lastUpdated = time.time() self.running = True self.message_log = [] self.cached_map = []
def refreshCachedMap(self): self.cached_map = [] for col in xrange(self.map_size_x): row_arr = [] for row in xrange(self.map_size_y): row_arr.append([]) self.cached_map.append(row_arr) for e, component in EntityManager.Instance().pairs_for_type( Transform2D): self.cached_map[component.position.x][component.position.y].append( e)
def onKeyPressed(self, args): char = args["char"] v = None GameManager.Instance().message("You pressed a button: {}".format(char), Colors.green) if char == "UP": v = Vector2D(0, -1) elif char == "LEFT": v = Vector2D(-1, 0) elif char == "RIGHT": v = Vector2D(1, 0) elif char == "DOWN": v = Vector2D(0, 1) elif char == "S": for e, component in EntityManager.Instance().pairs_for_type(Robot): EventManager.Instance().fireEvent("EVENT_ShootProjectile", {"origin" : e.components["Transform2D"].position, "direction" : e.components["Robot"].bot.direction, "damage" : 20}) return elif char == "1": robots = [] for e, component in EntityManager.Instance().pairs_for_type(Robot): robots.append(e) if len(robots) >= 1: EventManager.Instance().fireEvent("EVENT_FocusCameraOnEntity", robots[0]) return elif char == "2": robots = [] for e, component in EntityManager.Instance().pairs_for_type(Robot): robots.append(e) if len(robots) >= 2: EventManager.Instance().fireEvent("EVENT_FocusCameraOnEntity", robots[1]) return elif char == "G": EventManager.Instance().fireEvent("EVENT_FocusCameraOnEntity", None) return else: return for e, component in EntityManager.Instance().pairs_for_type(Robot): EventManager.Instance().fireEvent("EVENT_MoveEntity", {"entity" : e, "vector2D" : v})
def draw(self): zCache = {} for entity, transform in EntityManager.Instance().pairs_for_type(Transform2D): position = transform.position + (self.parent.console_mid - self.parent.offset_transform.position) # Only draw characters within bounds of screen if vectorInRange(position, 0, self.width, 0, self.height): key = "{0},{1}".format(transform.position.x, transform.position.y) if key not in zCache or entity.z >= zCache[key]: self.console.draw_char(position.x, position.y, entity.symbol) zCache[key] = entity.z # Blit the contents of "console" to the root console self.window.blit(self.console, 0, 0, self.width, self.height, 0, 0)