Changeset 312:72aca01c87ed for nagslang


Ignore:
Timestamp:
Sep 5, 2013, 10:37:26 PM (7 years ago)
Author:
David Sharpe
Branch:
default
rebase_source:
67f0519586bae9c6f558d1d92832a3d3f4cb18cc
Message:

Basic claw attack, stealing liberally from other people's code! ;)

Location:
nagslang
Files:
4 edited

Legend:

Unmodified
Added
Removed
  • nagslang/events.py

    r308 r312  
    6060        super(EnemyDeathEvent, cls).post(position=position,
    6161                                         enemy_type=enemy_type)
     62
     63class ClawEvent(UserEvent):
     64    @classmethod
     65    def post(cls, source, vector, source_collision_type):
     66        super(ClawEvent, cls).post(source=source, vector=vector,
     67                                   source_collision_type=source_collision_type)
     68
  • nagslang/game_object.py

    r307 r312  
    33
    44import math
     5import time
    56
    67from nagslang import environment
     
    1011from nagslang.constants import (
    1112    SWITCH_PUSHERS, COLLISION_TYPE_SWITCH, COLLISION_TYPE_BOX, ZORDER_LOW,
    12     ZORDER_FLOOR, COLLISION_TYPE_DOOR, COLLISION_TYPE_PROJECTILE)
     13    ZORDER_FLOOR, COLLISION_TYPE_DOOR, COLLISION_TYPE_PROJECTILE,
     14    COLLISION_TYPE_WEREWOLF_ATTACK)
    1315from nagslang.resources import resources
    1416from nagslang.events import DoorEvent
     
    405407            render.ImageRenderer(resources.get_image('objects', 'bullet.png')),
    406408        )
     409
     410
     411class ClawAttack(GameObject):
     412    def __init__(self, space, position, vector, source_collision_type):
     413        body = make_body(1, pymunk.inf, position)
     414        self.last_position = position
     415        self.shape = pymunk.Circle(body, 30)
     416        self.shape.sensor = True
     417        self.shape.collision_type = COLLISION_TYPE_WEREWOLF_ATTACK
     418        self.source_collision_type = source_collision_type
     419        super(ClawAttack, self).__init__(
     420            SingleShapePhysicser(space, self.shape),
     421            render.ImageRenderer(resources.get_image
     422                                ('objects', 'werewolf_SW_claw_attack.png')),
     423        )
     424        self.time_created = time.time()
     425
     426    def animate(self):
     427        super(ClawAttack, self).animate()
     428        position = (self.physicser.position.x, self.physicser.position.y)
     429        r = self.get_space().segment_query(self.last_position, position)
     430        self.last_position = position
     431        for collision in r:
     432            shape = collision.shape
     433            if (shape.collision_type == self.source_collision_type
     434                or shape == self.physicser.get_shape()
     435                or shape.sensor):
     436                continue
     437            if hasattr(shape, 'physicser'):
     438                shape.physicser.game_object.hit(self)
     439        if time.time() - self.time_created > 0.2:
     440            self.physicser.remove_from_space()
     441            self.remove = True
     442
  • nagslang/protagonist.py

    r309 r312  
    77    COLLISION_TYPE_PLAYER, ZORDER_MID, WEREWOLF_SOAK_FACTOR,
    88    PROTAGONIST_HEALTH_MIN_LEVEL, PROTAGONIST_HEALTH_MAX_LEVEL,
    9     NON_GAME_OBJECT_COLLIDERS)
    10 from nagslang.events import FireEvent
     9    NON_GAME_OBJECT_COLLIDERS, COLLISION_TYPE_WEREWOLF_ATTACK)
     10from nagslang.events import FireEvent,ClawEvent
    1111from nagslang.game_object import GameObject, Physicser, make_body
    1212from nagslang.mutators import FLIP_H
     
    253253        """
    254254        if self.in_wolf_form():
    255             pass
     255            self.claw()
    256256        else:
    257257            self.shoot()
     
    264264        vec.length = 1000
    265265        FireEvent.post(self.physicser.position, vec, 10, COLLISION_TYPE_PLAYER)
     266
     267    def claw(self):
     268        vec = Vec2d.unit()
     269        vec.angle = self.angle
     270        vec.length = 100
     271        ClawEvent.post(self.physicser.position, vec,
     272                       COLLISION_TYPE_WEREWOLF_ATTACK)
     273        print "Claw", self.physicser.position, vec
    266274
    267275    def in_wolf_form(self):
  • nagslang/screens/area.py

    r308 r312  
    88    COLLISION_TYPE_WALL, COLLISION_TYPE_PLAYER, CALLBACK_COLLIDERS)
    99from nagslang.enemies import DeadEnemy
    10 from nagslang.events import ScreenChange, DoorEvent, FireEvent, EnemyDeathEvent
     10from nagslang.events import ScreenChange, DoorEvent, FireEvent, EnemyDeathEvent, ClawEvent
    1111from nagslang.level import Level
    1212from nagslang.screens.base import Screen
    13 from nagslang.game_object import Bullet
     13from nagslang.game_object import Bullet, ClawAttack
    1414from nagslang.sound import sound
    1515
     
    157157                                   ev.enemy_type)
    158158            self._drawables.add(dead_enemy)
     159        elif ClawEvent.matches(ev):
     160            claw_attack = ClawAttack(self.space, ev.source, ev.vector,
     161                                     ev.source_collision_type)
     162            self._drawables.add(claw_attack)
    159163        self.keys.handle_event(ev)
    160164
Note: See TracChangeset for help on using the changeset viewer.