Beispiel #1
0
 def ai_move_to_target(self, *args, **kwargs):
     mt = self.move_target
     if mt:
         d = get_dist(
             mt.x,
             mt.y,
             self.x,
             self.y
         )
         if d > self.get_stat("arng"):
             line_of_sight = True
             line = (
                 (self.x, self.y), (mt.x, mt.y)
             )
             raycast_objects = self.game.spatial_hash.get_objects_from_line(
                 *line,
                 type=Collidable
             )
             for o in raycast_objects:
                 for s in o.body.shapes:
                     if s.segment_query(*line):
                         line_of_sight = False
                         break
             if line_of_sight:
                 self.movement.target = (mt.x, mt.y)
             else:
                 self.movement.target = None
                 self.brain.pop_state()
         else:
             self.brain.pop_state()
     else:
         self.brain.pop_state()
 def run(self):
     if getattr(self.o, "attacktarget"):
         t = self.o.attacktarget.who
         d = get_dist(
             t.position.x, t.position.y,
             self.o.position.x, self.o.position.y
         )
         if d <= 32:
             return True
         else:
             if getattr(self.o, "autoattacktarget"):
                 delattr(self.o, "autoattacktarget")
Beispiel #3
0
 def process(self, world, sets):
     for at, *rest in sets:
         e = world.get_entities(at)[0]
         dist = get_dist(
             e.position.x, e.position.y,
             at.who.position.x, at.who.position.y
         )
         if getattr(e, "autoattacktarget"):
             if dist > 32:
                 delattr(e, "autoattacktarget")
         elif dist <= 32:
             e.autoattacktarget = AutoAttackTarget(target=at.who)
Beispiel #4
0
 def process(self, world, sets):
     for ft, m, p, pb in sets:
         if ft.who and pb.body:
             if (
                 get_dist(
                     ft.who.position.x, ft.who.position.y,
                     p.x, p.y
                 ) > ft.range
             ):
                 velx = ft.who.position.x - p.x
                 vely = ft.who.position.y - p.y
                 pb.body.velocity.x += velx / 10
                 pb.body.velocity.y += vely / 10
Beispiel #5
0
 def ai_idle(self):
     if self.game.player:
         d = get_dist(
             self.game.player.x,
             self.game.player.y,
             self.x,
             self.y
         )
         if d <= 100:
             self.attack_target = self.game.player
             self.brain.push_state(self.ai_attack)
     else:
         self.move_target = None
         self.attack_target = None
         self.auto_attack_target = None
Beispiel #6
0
 def process(self, world, sets):
     # p = world.get_player()
     enemies = world.combined_components((IsMob, Position))
     # print(enemies)
     for player, mc in sets:
         for e, pos in enemies:
             if (
                 get_dist(
                     mc.x, mc.y,
                     pos.x, pos.y
                 ) <= 32
             ):
                 if mc.button == 1:
                     print("Targeted.")
                 elif mc.button == 4:
                     print("Attack!")
                 mc.handled = True
Beispiel #7
0
    def process(self, world, sets):
        if self.interval_counter >= self.interval:
            # print("Now!")
            self.interval_counter = 0
            for st, a in sets:
                o = world.get_entities(st)[0]
                pos = getattr(o, "physbody")
                if pos:
                    if pos.body:
                        p1 = pos.body.position.x, pos.body.position.y
                        for tpos, ta in world.combined_components(
                            (PhysBody, Allegiance)
                        ):
                            if not ta.value == a.value:
                                if tpos.body:
                                    p2 = (
                                        tpos.body.position.x,
                                        tpos.body.position.y
                                    )
                                    if get_dist(
                                        *p1, *p2
                                    ) <= 150:
                                        for s in pos.body.shapes:
                                            pos_old = s.group
                                            s.group = 2
                                        for s in tpos.body.shapes:
                                            tpos_old = s.group
                                            s.group = 2

                                        ps = world.phys_space
                                        c = ps.segment_query_first(
                                            p1, p2, group=2
                                        )
                                        for s in pos.body.shapes:
                                            s.group = pos_old
                                        for s in tpos.body.shapes:
                                            s.group = tpos_old
                                        # print(c)
                                        if not c:
                                            t = world.get_entities(tpos)[0]
                                            o.attacktarget = AttackTarget(t)
                                            delattr(o, "searchingtarget")
                                            print("Found target!")
        else:
            self.interval_counter += world.dt
Beispiel #8
0
 def ai_attack(self):
     if self.attack_target:
         if self.attack_target.hp <= 0:
             self.brain.pop_state()
             self.attack_target = None
             self.auto_attack_target = None
         else:
             d = get_dist(
                 self.attack_target.x,
                 self.attack_target.y,
                 self.x,
                 self.y
             )
             if d < 40:
                 if not self.auto_attack_target:
                     self.auto_attack_target = self.attack_target
             else:
                 self.move_target = self.attack_target
                 self.brain.push_state(self.ai_move_to_target)
     else:
         self.brain.pop_state()