Пример #1
0
class Base(ShowBase):
    def __init__(self):
        ShowBase.__init__(self)
        add_device_listener(
            config_file='keybindings.toml',
            assigner=SinglePlayerAssigner(),
        )
        base.disableMouse()
        self.sound = SoundManager()
        self.linefx = LineEffects()
        self.cardmaker = CardMaker("card")
        self.cardmaker.set_frame(-1,1,-1,1)
        self.turn_speed = 0.3 #length of turn animation in seconds
        self.sequence_player = SequencePlayer()
        self.transition = Transitions(loader)
        self.interface = Interface()
        self.bg_color = VBase4(0, 0, 0, 1)
        self.innitialize_fov()

        card, scene, camera, self.buffer = self.make_render_card()
        card.set_x(-0.25)
        self.camera = camera
        self.load_icons()

        self.texts = Texts(camera)
        self.pause = True
        self.instruct = True
        self.gameover = False
        self.won = False

        self.player = Player((0,0,0))
        self.map = Map()
        self.map.new_game()

        camera.reparent_to(self.player.root)
        camera.set_pos(4,-4,8)
        camera.look_at(self.player.root)
        camera.set_compass()
        base.task_mgr.add(self.update)

        card, scene, camera, buffer = self.make_render_card([3,7],[64,256],(0,100))
        self.hudgun = Actor("assets/models/hand.bam")
        self.hudgun.reparent_to(scene)
        self.hudgun.find("**/hand_healthy").show()
        self.hudgun.find("**/hand_hurt").hide()
        self.hudgun.setLODAnimation(1, 0.1, 0.005)
        self.player.weapon.set_hud_bullets()
        camera.look_at(self.hudgun)
        camera.set_pos(0.5,-1.5,10)
        camera.set_p(-90)
        card.set_scale(1/4,1,1)
        card.set_x(1-(1/4))
        self.quad = None
        self.setup_post_effect()

    def game_over(self):
        self.pause = True
        self.gameover = True
        self.texts.make_gameover()

    def win_game(self):
        self.pause = True
        self.won = True
        self.texts.make_end()

    def innitialize_fov(self):
        render.set_shader_auto()
        self.fov_point = PointLight("caster")
        self.fov_point.set_shadow_caster(True, 256*2, 256*2, -1000)
        self.fov_point.set_camera_mask(0b001)
        self.fov_point.set_lens_active(4, False)
        self.fov_point.set_lens_active(5, False)
        for i in range(6):
            self.fov_point.get_lens(i).set_near_far(0.5, 10)
        self.fov_point_np = render.attach_new_node(self.fov_point)
        self.fov_point_np.set_z(0.5)
        self.fov_point.set_color(VBase4(1,1,1,1))


    def make_render_card(self, ortho_size=[8,5], resolution=[256,256], nearfar=(5,40)):
        scene = NodePath("Scene")
        buffer = base.win.make_texture_buffer("Buffer", resolution[0], resolution[1])
        texture = buffer.get_texture()
        texture.set_magfilter(SamplerState.FT_nearest)
        texture.set_minfilter(SamplerState.FT_nearest)
        texture.set_wrap_u(SamplerState.WM_clamp)
        texture.set_wrap_v(SamplerState.WM_clamp)
        buffer.set_sort(-100)
        buffer.set_clear_color_active(True)
        buffer.set_clear_color(self.bg_color)
        camera = base.make_camera(buffer)
        lens = OrthographicLens()
        lens.set_film_size(ortho_size[0], ortho_size[1])
        lens.set_near(nearfar[0])
        lens.set_far(nearfar[1])
        camera.node().set_lens(lens)
        camera.reparent_to(scene)
        card = render2d.attach_new_node(self.cardmaker.generate())
        card.set_texture(texture)
        return card, scene, camera, buffer

    def setup_post_effect(self):
        self.manager = FilterManager(base.win, base.cam2d)
        tex = Texture()
        #tex = loader.load_texture("assets/noise.png")
        self.quad = self.manager.renderSceneInto(colortex=tex)
        self.quad.setShader(Shader.load(Shader.SL_GLSL, "crt.vert","crt.frag"))
        self.quad.setShaderInput("iResolution", (base.win.getXSize(), base.win.getYSize()))
        self.quad.setShaderInput("pattern", base.loader.load_texture("assets/crt.png"))
        self.quad.get_texture().set_wrap_u(SamplerState.WM_clamp)
        self.quad.get_texture().set_wrap_v(SamplerState.WM_clamp)
        base.accept("window-event", self.on_window_event)

    def on_window_event(self, win):
        base.windowEvent(win)
        self.quad.setShaderInput("iResolution", (base.win.getXSize(), base.win.getYSize()))

    def load_icons(self):
        model = loader.load_model("assets/models/icons.bam")
        self.icons = {}
        for child in model.get_children():
            child.clear_transform()
            child.detach_node()
            self.icons[child.name] = child

    def update(self, task):
        self.dt = globalClock.get_dt()
        context = base.device_listener.read_context('ew')
        if context["quit"]:
            sys.exit()
        if not self.pause:
            if self.player.alive:
                base.texts.deactivate()
            if not self.sequence_player.parallel:
                self.interface.update(context)
        elif not self.sequence_player.parallel:
            if self.won:
                return task.cont

            if context["select"]:
                self.texts.deactivate()
                if self.instruct:
                    self.instruct = False
                    self.texts.make_instructions()
                else:
                    self.pause = False
                    if self.gameover:
                        base.sound.stop("die")
                        self.gameover = False
                        self.map.destroy()
                        self.map.new_game()
                    else:
                        base.sound.music["opening"].stop()
                        base.sound.music["background"].play()
        return task.cont
Пример #2
0
class TheGameWindow(ShowBase):
    SPEED = 50

    def __init__(self, *args, **kwargs):
        super().__init__(*args, **kwargs)

        self.set_background_color(0.2, 0.2, 0.2, 0.6)
        # disables the default mouse camera control
        # self.disable_mouse()
        self.cam.set_pos(0, -100, 0)

        # lights
        self.plight = PointLight("pl")
        self.plight.set_color((1, 1, 1, 1))
        self.plight.set_color((1, 1, 1, 1))
        self.plight.set_attenuation((1, 0, 0))
        self.plnp = self.render.attach_new_node(self.plight)
        self.plnp.set_pos(0, 0, 100)
        self.render.set_light(self.plnp)

        self.amblight = AmbientLight("ambl")
        self.amblight.set_color((0.1, 0.1, 0.1, 1))
        self.ambnp = self.render.attach_new_node(self.amblight)
        self.render.set_light(self.ambnp)

        # shadows
        self.plight.set_shadow_caster(True, 2048, 2048)
        self.render.set_shader_auto()

        # floor
        self.floor = self.loader.loadModel(str(settings.egg / "floor.egg"))
        self.floor.reparent_to(self.render)
        self.floor.set_name("floor")

        self.sphere = self.loader.loadModel("models/misc/sphere")
        self.sphere.set_pos(0, 0, 20)
        self.sphere.set_name("sphere")
        self.sphere.set_scale(3.0)
        self.sphere.set_color((1, 1, 1, 1))
        self.sphere.reparent_to(self.render)

        # key assigment
        self.accept("w", update_key_map, ["w", True])
        self.accept("w-up", update_key_map, ["w", False])

        self.accept("s", update_key_map, ["s", True])
        self.accept("s-up", update_key_map, ["s", False])

        self.accept("a", update_key_map, ["a", True])
        self.accept("a-up", update_key_map, ["a", False])

        self.accept("d", update_key_map, ["d", True])
        self.accept("d-up", update_key_map, ["d", False])

        self.accept("e", update_key_map, ["e", True])
        self.accept("e-up", update_key_map, ["e", False])

        self.accept("q", update_key_map, ["q", True])
        self.accept("q-up", update_key_map, ["q", False])

        # collision
        self.cTrav = CollisionTraverser()
        self.col_pusher = CollisionHandlerPusher()

        # self.handler = CollisionHandlerEvent()
        # self.handler.addInPattern('%fn-into-%in')
        # self.accept('player-into-Floor', self.handle_collision)

        sphere_collider = self.sphere.attachNewNode(CollisionNode('player'))
        sphere_collider.node().addSolid(CollisionSphere(0, 0, 0, 1))
        sphere_collider.show()

        # floor_collider = self.floor.attachNewNode(CollisionNode('ground_zero'))
        # floor_collider.node().addSolid(CollisionPlane(Plane(Vec3(0, 0, 1), Point3(0, 0, 0))))
        # floor_collider.show()

        self.col_pusher.addCollider(sphere_collider, self.sphere)
        self.cTrav.showCollisions(self.render)
        # self.cTrav.addCollider(sphere_collider, self.handler)
        self.cTrav.addCollider(sphere_collider, self.col_pusher)

        # uppdate
        self.task_mgr.add(self.update)

    def handle_collision(self, entry):
        print(self.floor.get_name())
        for k in KEY_MAP:
            KEY_MAP[k] = False
        print(entry)

    def update(self, task: Task):
        dt = globalClock.getDt()

        if KEY_MAP["e"]:
            pos = self.sphere.get_pos()
            pos.z += self.SPEED * dt
            self.sphere.set_pos(pos)

        if KEY_MAP["q"]:
            pos = self.sphere.get_pos()
            pos.z -= self.SPEED * dt
            self.sphere.set_pos(pos)

        if KEY_MAP["a"]:
            pos = self.sphere.get_pos()
            pos.x -= self.SPEED * dt
            self.sphere.set_pos(pos)

        if KEY_MAP["d"]:
            pos = self.sphere.get_pos()
            pos.x += self.SPEED * dt
            self.sphere.set_pos(pos)

        if KEY_MAP["w"]:
            pos = self.sphere.get_pos()
            pos.y += self.SPEED * dt
            self.sphere.set_pos(pos)

        if KEY_MAP["s"]:
            pos = self.sphere.get_pos()
            pos.y -= self.SPEED * dt
            self.sphere.set_pos(pos)

        return task.cont
Пример #3
0
    def __init__(self, framework, *args, **kwargs):
        self.bullets = []
        self.environment = {}
        self.colliders = {}
        self.task_manager = []
        self.bullet_colliders = []
        self.remove_bullet_indices = []

        super().__init__(*args, **kwargs)
        self.f = framework
        self.controller = Controller(self.f.accept, self)
        self.f.set_background_color(0.2, 0.2, 0.2, 0.6)

        if settings.ALLOW_FILTERS:
            self.filter = CommonFilters(self.f.win, self.f.cam)
            self.filter.set_bloom(mintrigger=0.99, size="small")
            # self.filter.set_blur_sharpen(amount=0.99)

        self.base_node = NodePath("base")
        self.base_node.reparent_to(self.f.render)

        # floor
        self.plane = self.create_model(settings.egg / "plane",
                                       self.base_node,
                                       "ground-zero",
                                       color=(1, 1, 1, 1))
        self.create_model(settings.egg / "house_01",
                          self.base_node,
                          "house-01",
                          position=(20, 50, 0),
                          scale=1.5)
        self.create_model(settings.egg / "house_02",
                          self.base_node,
                          "house-02",
                          position=(-50, 10, 0),
                          scale=1.5)

        self.sun_light_model = self.create_model("models/misc/sphere",
                                                 self.base_node,
                                                 "sun",
                                                 position=(0, -150, 250),
                                                 scale=5.0,
                                                 color=(1, 1, 1, 1),
                                                 _store=False)

        tank = self.create_model(settings.egg / "tank",
                                 self.base_node,
                                 "player",
                                 scale=3.0,
                                 position=(0.0, 0.0, 1e-3))
        gun = self.create_model("models/misc/sphere",
                                tank,
                                "player-gun",
                                position=(0.0, 4.5, 1.755),
                                scale=0.1,
                                _store=False)
        self.tank = Tank(tank, gun, framework=self.f)
        self._tank = self.tank.tank

        enemy = self.create_model(settings.egg / "enemy",
                                  self.base_node,
                                  "enemy",
                                  scale=0.8,
                                  position=(-80.0, 80.0, 1e-3))
        self.enemy = EnemyTank(enemy, None, framework=self.f)

        # point light for shadows
        sun_light = PointLight("sun-light")
        sun_light.set_color((0.4, 0.4, 0.4, 1))
        self.sun_light_node = self.base_node.attach_new_node(sun_light)
        self.sun_light_node.reparent_to(self.sun_light_model)
        self.redistribute_light(self.sun_light_node)
        self.plane.set_light(self.sun_light_node)

        # daylight
        if settings.ALLOW_DAYLIGHT:
            day_light = DirectionalLight("day-light")
            day_light.set_color((1, 1, 1, 1))
            self.daylight_node = self.f.render.attach_new_node(day_light)
            self.daylight_node.set_p(-20)
            self.daylight_node.set_h(10)
            self.daylight_node.reparent_to(self.sun_light_model)
            self.redistribute_light(self.daylight_node)

        # shadows
        if settings.ALLOW_SHADOWS:
            sun_light.set_shadow_caster(True, 2048, 2048)
            self.f.render.set_shader_auto()

        # 3rd person camera lock
        if settings.TRD_PERSON_CAM:
            self.f.camera.reparent_to(self.tank.tank)
            self.f.cam.set_pos(0, -25, 10)
            self.f.cam.lookAt(self.tank.tank)
            self.f.cam.setP(self.f.camera.getP() - 20)

        if settings.ALLOW_AMBIENT:
            amblight = AmbientLight("ambient-light")
            amblight.set_color((0.2, 0.2, 0.2, 1))
            self.amblight_node = self.f.render.attach_new_node(amblight)
            self.redistribute_light(self.amblight_node)

        # colliders
        tank_collider = self.environment["player"].attachNewNode(
            CollisionNode('player-collider'))
        tank_collider.node().addSolid(
            CollisionBox(Point3(-1.5, -3, 0), Point3(1.5, 3, 2.25)))
        self.colliders['player-collider'] = tank_collider

        enemy_collider = self.environment["enemy"].attachNewNode(
            CollisionNode('enemy-collider'))
        enemy_collider.node().addSolid(
            CollisionBox(Point3(-6.5, -13.5, 0), Point3(6.5, 13, 10)))
        self.colliders['enemy-collider'] = enemy_collider

        house_01_collider = self.environment["house-01"].attachNewNode(
            CollisionNode('house-01-collider'))
        house_01_collider.node().addSolid(
            CollisionBox(Point3(0, 2, 0), Point3(14.5, 16, 17)))
        self.colliders['house-01'] = house_01_collider

        house_02_collider = self.environment["house-02"].attachNewNode(
            CollisionNode('house-02-collider'))
        house_02_collider.node().addSolid(
            CollisionBox(Point3(0, 0, 0), Point3(21, 27, 37)))
        self.colliders['house-02'] = house_02_collider

        floor_collider = self.environment["ground-zero"].attachNewNode(
            CollisionNode('ground-zero-collider'))
        floor_collider.node().addSolid(
            CollisionPlane(Plane(Vec3(0, 0, 1), Point3(0, 0, 0))))
        self.colliders['ground-zero-collider'] = floor_collider

        self.f.pusher.addCollider(tank_collider, self.environment["player"])
        self.f.cTrav.addCollider(tank_collider, self.f.pusher)

        # show.hide all colliders
        if settings.SHOW_COLLIDERS:
            for key, value in self.colliders.items():
                logger.info(f"collider for {key} is visible")
                value.show()
                self.f.cTrav.showCollisions(self.f.render)

        # uppdate
        self.f.cHandler.addInPattern('%fn')
        self.f.accept('bullet-collider', self.handle_collision)

        self.task_manager.append(self.f.task_mgr.add(self.update))
        self.task_manager.append(self.f.task_mgr.add(self.update_bullets))
Пример #4
0
    def __init__(self, *args, **kwargs):
        super().__init__(*args, **kwargs)
        self.set_background_color(0.2, 0.2, 0.2, 0.6)
        # self.render.setAntialias(AntialiasAttrib.MMultisample)
        self.controller = Controller(self.accept)
        self.cam.set_pos(0, -200, 10)

        self.player = self.loader.loadModel(str(settings.egg / "a2.egg"))
        self.player.set_pos(0, 0, 0)
        self.player.set_scale(3.0)
        self.player.set_color((0, 0, 0, 1))
        self.player.reparent_to(self.render)

        self.plane = self.loader.loadModel(str(settings.egg / "plane.egg"))
        self.plane.reparent_to(self.render)
        self.plane.set_name("plane")

        self.cube_01 = self.loader.loadModel(str(settings.egg / "bd-cube.egg"))
        self.cube_01.reparent_to(self.render)
        self.cube_01.set_name("cube")
        self.cube_01.set_scale(20)
        self.cube_01.set_pos(-100, 30, 20)

        self.cube_02 = self.loader.loadModel(str(settings.egg / "bd-cube.egg"))
        self.cube_02.reparent_to(self.render)
        self.cube_02.set_name("cube")
        self.cube_02.set_scale(10)
        self.cube_02.set_pos(-30, 100, 10)

        # ##############################################################################################################

        # player
        self.light_model = self.loader.loadModel("models/misc/sphere")
        self.light_model.set_pos(0, 0, 20)
        self.light_model.set_scale(3.0)
        self.light_model.set_color((1, 1, 1, 1))
        self.light_model.reparent_to(self.render)

        # lights
        if True:
            day_light = DirectionalLight("sun")
            day_light.set_color((1, 1, 1, 1))
            self.sun = self.render.attach_new_node(day_light)
            self.sun.set_p(-20)
            self.sun.set_h(10)

            self.cube_01.set_light(self.sun)
            self.cube_02.set_light(self.sun)
            self.player.set_light(self.sun)
            self.plane.set_light(self.sun)

        sun_spotlight = PointLight("sun_spotlight")
        sun_spotlight.set_color((1, 1, 1, 1))
        self.sun_spotlight_nodepath = self.render.attach_new_node(sun_spotlight)
        self.sun_spotlight_nodepath.reparent_to(self.light_model)

        self.cube_01.set_light(self.sun_spotlight_nodepath)
        self.cube_02.set_light(self.sun_spotlight_nodepath)
        self.player.set_light(self.sun_spotlight_nodepath)
        self.plane.set_light(self.sun_spotlight_nodepath)

        amblight = AmbientLight("ambient_light")
        amblight.set_color((0.01, 0.01, 0.01, 1))
        self.ambnp = self.render.attach_new_node(amblight)
        self.cube_01.set_light(self.ambnp)
        self.cube_02.set_light(self.ambnp)
        self.player.set_light(self.ambnp)
        self.plane.set_light(self.ambnp)

        sun_spotlight.set_shadow_caster(True, 2048, 2048)
        self.render.set_shader_auto()

        # shadows
        # self.day_light.set_shadow_caster(True, 2048, 2048)
        # self.render.set_shader_auto()

        # uppdate
        self.task_mgr.add(self.update)
Пример #5
0
class TheGameWindow(ShowBase):
    def __init__(self, *args, **kwargs):
        super().__init__(*args, **kwargs)
        self.speed = 50
        self.set_background_color(0, 0, 0, 0.6)
        # disables the default mouse camera control
        # self.disable_mouse()
        self.cam.set_pos(0, -100, 0)

        self.floor = self.loader.loadModel(str(settings.egg / "floor.egg"))
        self.bc304 = self.loader.loadModel(str(settings.egg / "bc304.egg"))

        self.floor.set_color((0.5, 0.5, 0.5, 1))
        self.floor.reparent_to(self.render)

        self.sphere = self.loader.loadModel("models/misc/sphere")
        self.sphere.reparent_to(self.render)
        self.sphere.set_pos(10, 10, 10)
        self.sphere.set_scale(2.0)

        self.amblight = AmbientLight("ambl")
        self.amblight.set_color((0.1, 0.1, 0.1, 1))
        self.ambnp = self.render.attach_new_node(self.amblight)

        self.plight = PointLight("pl")
        self.plight.set_shadow_caster(True, 2048, 2048)
        self.render.set_shader_auto()

        self.plight.set_color((1, 1, 1, 1))
        self.plight.set_attenuation((1, 0, 0))

        self.plnp = self.sphere.attach_new_node(self.plight)
        self.bc304.set_light(self.plnp)
        self.floor.set_light(self.plnp)

        self.bc304.set_light(self.ambnp)
        self.floor.set_light(self.ambnp)

        self.filter = CommonFilters(self.win, self.cam)
        self.filter.set_bloom(mintrigger=0.9, size="large")

        # left-rigth, far-from-camera, vertical-movement
        # x, y, z
        self.bc304.set_pos(0, 0, 50)
        self.bc304.set_scale(0.1)
        self.bc304.reparent_to(self.render)
        # heading, pitch,, roll
        self.bc304.set_hpr(280, 10, 0)

        self.x, self.y, self.z = 0.0, 0.0, 0.0
        self.h, self.p = 0.0, 0.0
        self.task_mgr.add(self.update)

        # self.accept("mouse1", self.to_pegasus)
        self.accept("mouse1-up", self.to_pegasus)

        # self.accept("mouse2", self.to_pegasus)
        self.accept("mouse2-up", self.to_pegasus)

        # self.accept("mouse3", self.to_pegasus)
        self.accept("mouse3-up", self.to_pegasus)

        self.accept("arrow_left", update_key_map, ["left", True])
        self.accept("arrow_left-up", update_key_map, ["left", False])

        self.accept("arrow_right", update_key_map, ["right", True])
        self.accept("arrow_right-up", update_key_map, ["right", False])

        self.accept("arrow_up", update_key_map, ["up", True])
        self.accept("arrow_up-up", update_key_map, ["up", False])

        self.accept("arrow_down", update_key_map, ["down", True])
        self.accept("arrow_down-up", update_key_map, ["down", False])

        self.accept("space", update_key_map, ["space", True])
        self.accept("space-up", update_key_map, ["space", False])

        self.accept("w", update_key_map, ["w", True])
        self.accept("w-up", update_key_map, ["w", False])

        self.accept("s", update_key_map, ["s", True])
        self.accept("s-up", update_key_map, ["s", False])

        self.accept("a", update_key_map, ["a", True])
        self.accept("a-up", update_key_map, ["a", False])

        self.accept("d", update_key_map, ["d", True])
        self.accept("d-up", update_key_map, ["d", False])

        self.accept("e", update_key_map, ["e", True])
        self.accept("e-up", update_key_map, ["e", False])

        self.accept("q", update_key_map, ["q", True])
        self.accept("q-up", update_key_map, ["q", False])

    def to_pegasus(self):
        # mouse_pos = self.mouseWatcherNode.getMouse()
        # mouse_pos_3d = (mouse_pos[0], 0, mouse_pos[1])
        # mouse_pos_button = self.button.getRelativePoint(render, mouse_pos_3d)
        # print(mouse_pos_3d)

        # pointer = self.win.get_pointer(0)
        # self.z = -(pointer.get_y() - (720 / 2)) / 10
        # print(self.z)

        # self.bc304.set_pos(pointer.get_x(), 0, self.z)
        pass

    def update(self, task: Task):
        dt, ft = globalClock.getDt(), globalClock.getFrameTime()

        if KEY_MAP["up"]:
            self.bc304.set_pos(self.x, self.y, self.z)
            self.z += self.speed * dt

        if KEY_MAP["down"]:
            self.bc304.set_pos(self.x, self.y, self.z)
            self.z -= self.speed * dt

        if KEY_MAP["right"]:
            self.bc304.set_pos(self.x, self.y, self.z)
            self.x += self.speed * dt

        if KEY_MAP["left"]:
            self.bc304.set_pos(self.x, self.y, self.z)
            self.x -= self.speed * dt

        if KEY_MAP["e"]:
            pos = self.sphere.get_pos()
            pos.z += self.speed * dt
            self.sphere.set_pos(pos)

        if KEY_MAP["q"]:
            pos = self.sphere.get_pos()
            pos.z -= self.speed * dt
            self.sphere.set_pos(pos)

        if KEY_MAP["a"]:
            pos = self.sphere.get_pos()
            pos.x -= self.speed * dt
            self.sphere.set_pos(pos)

        if KEY_MAP["d"]:
            pos = self.sphere.get_pos()
            pos.x += self.speed * dt
            self.sphere.set_pos(pos)

        if KEY_MAP["w"]:
            pos = self.sphere.get_pos()
            pos.y += self.speed * dt
            self.sphere.set_pos(pos)

        if KEY_MAP["s"]:
            pos = self.sphere.get_pos()
            pos.y -= self.speed * dt
            self.sphere.set_pos(pos)

        # self.sphere.set_pos(float(np.cos(ft) * 4), float(np.sin(ft) * 4), float(np.sin(ft) * 10))

        # self.z -= 0.5 * dt
        # self.bc304.set_hpr(self.h, self.p, 0)
        # self.h += 0.1
        # self.p += 0.1

        return task.cont