def on_draw(self): red, green, blue = config.sky_color glClearColor(red, green, blue, 0) self.window.clear() glPushMatrix() glTranslatef(self.window.width // 2, self.window.height // 2, 0) scale = self.window.height / 15 glScalef(scale, scale, scale) camera_position = (self.icarus.body.position + b2.b2Vec2(*config.camera_offset)) camera_position.y = clamp(camera_position.y, config.camera_min_y, config.camera_max_y) glTranslatef(-camera_position.x, -camera_position.y, 0) for cloud in self.clouds: cloud.draw_shadow() self.draw_sea() self.island.draw() self.pearly_gates.render() rabbyt.render_unsorted(self.temples) self.icarus.draw() for cloud in self.clouds: cloud.draw() glPopMatrix() self.draw_fade() if config.fps: self.clock_display.draw() return pyglet.event.EVENT_HANDLED
def step_flying(self, dt): up = pyglet.window.key.UP in self.keys left = pyglet.window.key.LEFT in self.keys right = pyglet.window.key.RIGHT in self.keys if up or left or right: # Grow tired from flapping those wings. self.fatigue = (dt / config.flight_duration + clamp(self.fatigue, 0, 1)) if left ^ right: self.facing = right - left # Fatigue and damage affect flight capabilities. fatigue_factor = 1 - 2 * clamp(self.fatigue - 0.5, 0, 0.5) damage_factor = 1 - 2 * clamp(self.damage - 0.5, 0, 0.5) lift_force = up * fatigue_factor * damage_factor * config.icarus_lift_force side_force = (right - left) * config.icarus_side_force left = pyglet.window.key.LEFT in self.keys right = pyglet.window.key.LEFT in self.keys air_force = -(self.body.linearVelocity * config.icarus_air_resistance) self.body.ApplyForce(b2.b2Vec2(side_force, lift_force) + air_force, self.body.position) torque = -(self.body.angle * config.icarus_angular_k + self.body.angularVelocity * config.icarus_angular_damping) self.body.ApplyTorque(torque)
def draw_shadow(self): sun_position = b2.b2Vec2(*self.game_screen.sun.position) cloud_position = self.body.position top_left = cloud_position - b2.b2Vec2(self.width / 2, 0) top_right = cloud_position + b2.b2Vec2(self.width / 2, 0) left_slope = top_left - sun_position left_slope.Normalize() right_slope = top_right - sun_position right_slope.Normalize() bottom_left = top_left + left_slope * config.shadow_length bottom_right = top_right + right_slope * config.shadow_length glBindTexture(GL_TEXTURE_2D, 0) glBegin(GL_QUADS) red, green, blue = config.shadow_color glColor4f(red, green, blue, 1) glVertex2f(top_left.x, top_left.y) glVertex2f(top_right.x, top_right.y) glColor4f(red, green, blue, 0) glVertex2f(bottom_right.x, bottom_right.y) glVertex2f(bottom_left.x, bottom_left.y) glEnd()
def step_walking(self, dt): # Rest on the ground. self.fatigue = clamp(self.fatigue, 0, 1) - dt / config.rest_duration left = pyglet.window.key.LEFT in self.keys right = pyglet.window.key.RIGHT in self.keys if not left and not right: self.state = 'standing' return if left ^ right: self.facing = right - left force = b2.b2Vec2(right - left, 0) * 10 - self.body.linearVelocity self.body.ApplyForce(force, self.body.position) torque = -(self.body.angle * config.icarus_angular_k + self.body.angularVelocity * config.icarus_angular_damping) self.body.ApplyTorque(torque)
def update_state(self): if (not self.immortal and (self.damage >= 1 or self.fatigue >= 1) or self.body.position.y <= 0): self.state = 'falling' elif pyglet.window.key.UP in self.keys: self.state = 'flying' else: # See if there's any ground beneath Icarus's feet. segment = b2.b2Segment() segment.p1 = self.body.position segment.p2 = segment.p1 + b2.b2Vec2(0, -0.6) _, _, shape = self.game_screen.world.RaycastOne(segment, False, None) if shape is not None and not shape.isSensor: if self.state not in ('standing', 'walking'): self.state = 'standing' else: self.state = 'flying'
def draw(self): self.sprite.xy = (self.body.position + b2.b2Vec2(*config.island_offset)).tuple() self.sprite.render()
def update_sun_distance(self): sun_position = b2.b2Vec2(*self.game_screen.sun.position) self.sun_distance = (self.body.position - sun_position).Length()
def get_linear_velocity_in_point(self, body, point): offset = point - body.GetWorldCenter() return body.linearVelocity + b2.b2Vec2(-offset.y, offset.x) * body.angularVelocity