class MeleeWeaponEntity(entity.WorldEntity): name = "MeleeWeaponEntity" etype = "Projectile" def __init__(self, **kwargs): self.image = "sword.png" super(MeleeWeaponEntity, self).__init__() def _init_sprite(self, sprite): # TODO: Sprite still isnt centered on player sprite.transform_anchor = sprite.get_rect().midbottom def update(self, t): super(MeleeWeaponEntity, self).update(t) wielder = game.get_entity(self.wielder) self.position = wielder.position.copy() swing_v = float(self.arc) / self.duration self.rotation_off = self.rotation_off + swing_v * t self.rotation = wielder.rotation + self.rotation_off self.duration_left -= t if self.duration_left <= 0: self.die() entity.new_entity(MeleeWeaponEntity)
class BasicEnemy(npc): image = "player.png" name = "basicenemy" xp_worth = 20 attack_range = 0.5 # The attack_range is the reach from the edge of the body's shape, to the intended range in box2d meters def init_physics(self, world): super(npc, self).init_physics(world) r = self.size / constants.PIXEL_TO_METER + self.attack_range sensor = self.body.CreateCircleFixture(radius=r, isSensor=True) def callback(us, detected, dt): if not self.can_attack(): return else: for typ, other in detected: if not typ == "entity": continue other = game.Game.get_entity(other) if other and other.is_player: self.attack(other) game.Game.register_sensor(sensor, callback) #del(sensor) entity.new_entity(BasicEnemy)
# self.die() def update_collision(self): center = Vector2(*self.position) + util.rot_to_vec(self.rotation) * self.offset #print center, self.size return cm.CircleShape(center=center, r=self.size) def on_collision(self, other, typ): if typ == "wall": return success = False owner = game.Game.get_entity(self.controlled_by) if not owner: return if other is owner: return if not other.eid in self._already_hit: self._already_hit.append(other.eid) self.on_hit(other) def on_hit(self, other): other.take_damage(self.damage) entity.new_entity(MeleeWeaponEntity) entity.new_entity(Projectile)
return dr = target_aim - self.rotation v = 0 if abs(dr) >= 180: # Rotate clockwise v = -1 else: v = 1 self.rotation += min((self.turn_speed * t * v), dr) if self.rotation > 360: self.rotation -= 360 if self.rotation < 0: self.rotation += 360 def attack(self): if self.weapon: self.weapon.attack() self.attack_cooldown += self.weapon.attack_speed def update_input(self, state): self.attacking = state["attacking"] self.aim = state["aim"] self.move_dir = euclid.Vector2(*state["movement"]) if self.move_dir.magnitude() > 1: self.move_dir.normalize() # We only want the direction (at least when using a keyboard) entity.new_entity(Player)
import entity from cocos import collision_model as cm class Projectile(entity.WorldEntity): image = "arrow.png" etype = "projectile" name = "Projectile" def __init__(self, **kwargs): super(Projectile, self).__init__() def update(self, t): super(Projectile, self).update(t) self.duration -= t if self.duration <= 0: self.die() def update_collision(self): self.cshape = cm.CircleShape(center=self.position, r=self.size) entity.new_entity(Projectile)
self.damage = 200 def attack(self): real_wielder = self.get_wielder() # The real entity (not the eid) e = entity.get_entity_type("MeleeWeaponEntity")() e.damage = self.damage e.friendly = real_wielder.friendly e.attached_to = self.wielder e.wielder = self.wielder e.controlled_by = real_wielder.controlled_by e.position = real_wielder.position.copy() e.duration = self.p_duration e.duration_left = e.duration e.offset = self.offset e.arc = self.arc e.size = self.size e.rotation_off = -self.arc / 2 e.swing_speed = self.p_swing_speed e.width = self.p_width e.length = self.p_length game.Game.spawn(e) if game.Game.is_controlled(e): events.dispatch("on_attack", self.get_wielder(), self, e) weapons = (BowWeapon, MeleeWeapon) for w in weapons: entity.new_entity(w)