Beispiel #1
0
    def update(self):
        self._i += 1
        if self._i < self.update_rate:
            return

        self._i = 0

        for other in self.trigger_targets:
            if other == self:
                continue

            dist = distance(other, self)
            if not other in self.triggerers and dist <= self.radius:
                self.triggerers.append(other)
                if hasattr(self, 'on_trigger_enter'):
                    self.on_trigger_enter()
                continue

            if other in self.triggerers and dist > self.radius:
                self.triggerers.remove(other)
                if hasattr(self, 'on_trigger_exit'):
                    self.on_trigger_exit()
                continue

            if other in self.triggerers and hasattr(self, 'on_trigger_stay'):
                self.on_trigger_stay()
    def find_collision(self):
        self.collisions = list()
        self.collision = None
        if not self.raycast or self._pq.get_num_entries() == 0:
            self.unhover_everything_not_hit()
            return False

        self._pq.sortEntries()

        for entry in self._pq.getEntries():
            for entity in scene.entities:
                if entry.getIntoNodePath(
                ).parent == entity and entity.collision:
                    if entity.collision:
                        hit = Hit(
                            hit=entry.collided(),
                            entity=entity,
                            distance=distance(entry.getSurfacePoint(scene),
                                              camera.getPos()),
                            point=entry.getSurfacePoint(entity),
                            world_point=entry.getSurfacePoint(scene),
                            normal=entry.getSurfaceNormal(entity),
                            world_normal=entry.getSurfaceNormal(scene),
                        )
                        hit.point = Vec3(hit.point[0], hit.point[2],
                                         hit.point[1])
                        hit.world_point = Vec3(hit.world_point[0],
                                               hit.world_point[2],
                                               hit.world_point[1])
                        hit.normal = Vec3(hit.normal[0], hit.normal[2],
                                          hit.normal[1])
                        hit.world_normal = Vec3(hit.world_normal[0],
                                                hit.world_normal[2],
                                                hit.world_normal[1])
                        self.collisions.append(hit)
                        break

        if self.collisions:
            self.collision = self.collisions[0]
            self.hovered_entity = self.collision.entity
            if not self.hovered_entity.hovered:
                self.hovered_entity.hovered = True
                if hasattr(self.hovered_entity, 'on_mouse_enter'):
                    self.hovered_entity.on_mouse_enter()
                for s in self.hovered_entity.scripts:
                    if hasattr(s, 'on_mouse_enter'):
                        s.on_mouse_enter()

        self.unhover_everything_not_hit()