def survey_points(self): if self.agent.manager.tiles[self.agent.location].off_road: self.agent.off_road = True else: self.agent.off_road = False location = self.agent.location target = self.agent.target_tile start = mathutils.Vector([ location[0] + self.agent.tile_offset, location[1] + self.agent.tile_offset, 0.0 ]) start_key = bgeutils.get_key(start) self.start_hit = self.agent.manager.tiles[start_key] if target: end = mathutils.Vector([ target[0] + self.agent.tile_offset, target[1] + self.agent.tile_offset, 0.0 ]) end_key = bgeutils.get_key(end) self.end_hit = self.agent.manager.tiles[end_key] else: self.end_hit = self.start_hit if self.vehicle: if self.agent.on_screen and target: if self.last_hit != self.start_hit: self.trails.add_tracks(self.start_hit.normal) else: self.trails.end_tracks() self.last_hit = self.start_hit
def get_destination(self): self.set_speed() location = self.agent.box.worldPosition.copy() location.z = 0.0 offset = mathutils.Vector(self.agent.formation[self.man.index]).to_3d() offset.rotate(self.agent.hull.worldOrientation.copy()) destination = (location + offset) self.destination = bgeutils.get_key(destination)
def start_up(self): start = mathutils.Vector(self.location).to_3d() start_hit = self.agent.manager.tiles.get(bgeutils.get_key(start)) if start_hit: self.start = start_hit.point else: self.start = start self.get_destination() self.get_next_tile() self.frame_update()
def get_cursor_location(self): busy = self.select_point or self.movement_action if self.UI_mouse_over and not busy: self.context = "UI" elif self.mouse_timer > self.mouse_refresh: self.mouse_timer = 0 ground_hit = self.mouse_hit_ray("ground") if ground_hit[0]: self.tile_over = bgeutils.get_key(ground_hit[1]) contents = self.get_contents() if contents: self.mouse_over_unit = contents else: self.mouse_over_unit = None context = "PAN" if busy: self.context = "SELECT" if self.mouse_over_unit: if self.mouse_over_unit.team > 0: if self.mouse_over_unit.visible: if self.selected_agents: context = "TARGET" else: context = "NO_TARGET" else: if self.mouse_over_unit.team == 0: context = "SELECT" else: context = "NO_TARGET" self.context = context self.mouse_timer += 1
def set_position(self, position): self.invalid_location = False for axis in position: if axis < 0.0 or axis > self.manager.level_size * 8: self.invalid_location = True if self.invalid_location: position = position.to_3d() normal = mathutils.Vector([0.0, 0.0, 1.0]) else: ground_hit_position = self.manager.tiles[bgeutils.get_key( position)] position = ground_hit_position.point normal = ground_hit_position.normal self.object_box.worldPosition = position self.object_box.worldPosition.z += 0.5 self.object_box.alignAxisToVect(normal)
def update(self): if self.rotation_countdown > 0: self.rotation_countdown -= 1 else: vector_start = self.movement_point ground_hit = self.manager.tiles[bgeutils.get_key( self.manager.tile_over)] vector_end = ground_hit.point.to_2d() movement_vector = vector_end - vector_start local_vector = vector_end - self.center_point angle = movement_vector.angle_signed(local_vector, 0.0) for marker in self.movement_markers: rotation = mathutils.Euler((0.0, 0.0, angle)) new_position = marker.offset.copy().to_3d() new_position.rotate(rotation) target_point = self.movement_point.copy() - new_position.to_2d( ) marker.update(target_point)
def choose_tile(self): avoid = self.avoiding if len(self.history) > 15: self.history = [] search_array = [(1, 0), (1, 1), (0, 1), (1, -1), (-1, 0), (-1, 1), (0, -1), (-1, -1)] if avoid: reference = bgeutils.get_key(avoid.box.worldPosition.copy()) else: reference = self.destination current_tile = self.location target = current_tile choice = self.direction closest = 10000.0 furthest = 0.0 for s in search_array: neighbor = (current_tile[0] + s[0], current_tile[1] + s[1]) if not self.check_occupied(neighbor): if neighbor not in self.history: distance = (mathutils.Vector(reference) - mathutils.Vector(neighbor)).length if bgeutils.diagonal(s): distance += 0.4 if avoid: if distance > furthest: furthest = distance choice = s target = neighbor else: if distance < closest: closest = distance choice = s target = neighbor self.direction = choice self.target = target self.history.append(target) self.clear_occupied() self.set_occupied(self.target) start = mathutils.Vector(self.location).to_3d() end = mathutils.Vector(self.target).to_3d() start_hit = self.agent.manager.tiles[bgeutils.get_key(start)] end_hit = self.agent.manager.tiles[bgeutils.get_key(end)] if start_hit and end_hit: self.start = start_hit.point self.end = end_hit.point else: self.start = start self.end = end