def onDoor(self, obj1, obj2): obj1 = obj1.owner obj2 = obj2.owner if isinstance(obj1, Door): foo = obj1 obj1 = obj2 obj2 = foo if not isinstance(obj1, Player) or not isinstance(obj2, Door): return if obj1.id != gameplay.state.my_player_id: return gameplay.state.players[gameplay.state.my_player_id].setRoom( obj2.toX, obj2.toY) self.collider.pos = Vec2(500, 300) leftX = 64 + self.width / 2 midX = graphics.view.GAME_WIDTH / 2 rightX = graphics.view.GAME_WIDTH - 64 - self.width / 2 topY = 64 + self.height / 2 midY = graphics.view.WINDOW_SIZE[1] / 2 botY = graphics.view.WINDOW_SIZE[1] - 64 - self.height / 2 newXLoc = 300 newYLoc = 300 if obj2.dir == 0: newXLoc = midX newYLoc = botY elif obj2.dir == 1: newXLoc = leftX newYLoc = midY elif obj2.dir == 2: newXLoc = midX newYLoc = topY elif obj2.dir == 3: newXLoc = rightX newYLoc = midY self.collider.pos = Vec2(newXLoc, newYLoc) for collider in gameplay.state.floor.board[obj2.fromX][ obj2.fromY].simulation.objects: if isinstance(collider.owner, Player): gameplay.state.floor.board[obj2.fromX][ obj2.fromY].simulation.remove_object(collider) game_event_dict = { "type": "PLAYER", "code": "CHANGE_ROOM", "player_id": self.id, "pos": self.collider.pos.to_dict(), "velocity": self.collider.vel.to_dict(), "room_x": self.roomX, "room_y": self.roomY } event = GameEvent(game_event_dict) gameplay.state.global_queue.put(event)
def __init__(self, monster_id, x, y, type): self.id = monster_id self.type = type self.width = 40 self.height = 40 self.speed = 50 self.next_bullet_id = 0 self.sprites = [] self.load_sprite() self.collider = physics.PhysObject(Vec2(x, y), Polygon.square(x, y, self.width, self.height), self, collision_type=physics.collision_types.dynamic)
def __init__(self, dir, nextX, nextY, pastX, pastY): self.toX = nextX self.toY = nextY self.fromX = pastX self.fromY = pastY self.dir = dir leftX = 32 midX = graphics.view.GAME_WIDTH / 2 rightX = graphics.view.GAME_WIDTH - 32 topY = 32 midY = graphics.view.WINDOW_SIZE[1] / 2 botY = graphics.view.WINDOW_SIZE[1] - 32 if dir == 0: self.collider = physics.PhysObject( Vec2(midX, topY), Polygon.square(midX, topY, 64, 64), self, collision_type=physics.collision_types.trigger) elif dir == 1: self.collider = physics.PhysObject( Vec2(rightX, midY), Polygon.square(rightX, midY, 64, 64), self, collision_type=physics.collision_types.trigger) elif dir == 2: self.collider = physics.PhysObject( Vec2(midX, botY), Polygon.square(midX, botY, 64, 64), self, collision_type=physics.collision_types.trigger) elif dir == 3: self.collider = physics.PhysObject( Vec2(leftX, midY), Polygon.square(leftX, midY, 64, 64), self, collision_type=physics.collision_types.trigger) self.speed = 0 self.width = 64 self.height = 64 self.sprite = None self.loadSprite(dir)
def processPlayerEvent(self, event): if event.params["player_id"] != self.id: return self.collider.pos = Vec2.from_dict(event.params["pos"]) if event.params["code"] == "CHANGE_VELOCITY": self.collider.vel = Vec2.from_dict(event.params["velocity"]) elif event.params["code"] == "CHANGE_ROOM": self.collider.vel = Vec2.from_dict(event.params["velocity"]) self.setRoom(event.params["room_x"], event.params["room_y"]) elif (event.params["code"] == "SHOOT"): #each client is responsible for sending updates about its #player's bullets (and ONLY its player's bullets) #to the other clients every tick. #thus it is safe to have all clients execute the shoot function, #because the bullet's position will be quickly corrected if it gets #out of sync self.shoot(event.params["direction_x"], event.params["direction_y"])
def __init__(self): self.collider = physics.PhysObject( Vec2(300, 300), Polygon.square(300, 300, 16, 16), self, collision_type=physics.collision_types.trigger) self.width = 64 self.height = 64 self.speed = 0 self.collider.add_callback(self.onCollide) self.collider.vel.x = 0 self.collider.vel.y = 0 self.sprite = None self.loadSprite()
def __init__(self, bullet_id, x, y, direction_x, direction_y): print("shooting") self.direction_x = direction_x self.direction_y = direction_y self.id = bullet_id self.speed = 80 self.collider = physics.PhysObject( Vec2(x + direction_x * 5, y + direction_y * 5), Polygon.square(x + direction_x * 5, y + direction_y * 5, 10, 10), self, collision_type=physics.collision_types.player) self.collider.add_callback(self.onCollide) self.collider.vel.x = direction_x * self.speed self.collider.vel.y = direction_y * self.speed self.load_sprite() self.alive = True
def processInputEvent(self, event: sdl2.SDL_Event): # We'll want to return a GameEvent, and these fields # are the same regardless of what the rest of the event is game_event_dict = { "type": "PLAYER", "player_id": self.id, "pos": self.collider.pos.to_dict() } if event.type == sdl2.SDL_KEYDOWN or event.type == sdl2.SDL_KEYUP: state = ControlsState.state vel = Vec2() vel.x = state[sdl2.SDLK_d] - state[sdl2.SDLK_a] vel.y = state[sdl2.SDLK_s] - state[sdl2.SDLK_w] if (vel.length() > 0): vel = vel / vel.length() vel = vel * self.speed game_event_dict["code"] = "CHANGE_VELOCITY" game_event_dict["velocity"] = vel.to_dict() return GameEvent(game_event_dict) if (event.type == sdl2.SDL_MOUSEBUTTONDOWN): mouseX, mouseY = ctypes.c_int(0), ctypes.c_int( 0) # Create two ctypes values # Pass x and y as references (pointers) to SDL_GetMouseState() buttonstate = sdl2.mouse.SDL_GetMouseState(ctypes.byref(mouseX), ctypes.byref(mouseY)) x = mouseX.value - graphics.view.game_view.x y = mouseY.value - graphics.view.game_view.y game_event_dict["code"] = "SHOOT" direction_x = x - self.collider.pos.x direction_y = y - self.collider.pos.y length = math.sqrt(direction_x**2 + direction_y**2) direction_x = direction_x / length direction_y = direction_y / length game_event_dict["direction_x"] = direction_x game_event_dict["direction_y"] = direction_y #print(game_event_dict) return GameEvent(game_event_dict) game_event_dict["code"] = "NONE" return GameEvent(game_event_dict)
def make_walls(self): thickness = 64 width = 1000 - 188 height = 650 x, y = width // 2, 32 rect = Polygon.square(x, y, width - 100, 32) self.simulation.add_object(PhysObject(Vec2(x, y), rect, None)) y = height - 32 rect = Polygon.square(x, y, width - 100, 32) self.simulation.add_object(PhysObject(rect.pos, rect, None)) x, y = 32, height // 2 rect = Polygon.square(x, y, 32, height - 100) self.simulation.add_object(PhysObject(rect.pos, rect, None)) x = width - 32 rect = Polygon.square(x, y, 32, height - 100) self.simulation.add_object(PhysObject(rect.pos, rect, None))
def __init__(self, player_id, x, y): self.collider = physics.PhysObject( Vec2(x, y), Polygon.square(x, y, 30, 45), self, collision_type=physics.collision_types.player) self.collider.add_callback(self.onDoor) self.collider.add_callback(self.onEnemy) self.maxHealth = 500 self.currHealth = 500 self.lastMeleeHit = 0 self.width = 66 self.height = 93 self.speed = 200 self.id = player_id self.next_bullet_id = 0 self.sprite = None self.roomX = -1 self.roomY = -1 self.load_sprite()
def from_dict(dict): pos = Vec2.from_dict(dict["pos"]) m = Monster(dict["id"], pos.x, pos.y, dict["type"]) m.collider.vel = Vec2.from_dict(dict["vel"]) return m