Source code for objects.maneuvers

import math
from copy import deepcopy
from typing import TypeVar

import glm

import objects.aircraft as aircraft


[docs] class Maneuver: """ Maneuver base class Provide logic of maneuver simulation """ def __init__(self, duration: int, obj: 'aircraft.Aircraft'): if duration <= 0: raise Exception("Duration must be greater than zero.") self.duration = duration self.object: 'aircraft.Aircraft' = obj self.is_finished = False self.current_time: int = 0
[docs] def prepare(self): """ The method of performing the maneuver """ pass
[docs] def do(self): """ The method of performing the maneuver """ self.current_time += 1 if self.current_time >= self.duration: self.finish() self.is_finished = True
[docs] def finish(self): """ Performs the completion of the maneuver """ pass
[docs] class CenterFold(Maneuver): """ Class for centerfold maneouver """ def __init__(self, duration: int, obj: 'aircraft.Aircraft'): super().__init__(duration, obj) self.angle = math.pi / duration
[docs] def prepare(self): pass
[docs] def do(self): self.current_time += 1 if self.current_time >= self.duration: self.finish() self.is_finished = True abs_speed = abs(self.object.speed) self.object.speed.x = self.object.speed.x * math.cos(self.angle) - \ self.object.speed.y * math.sin(self.angle) self.object.speed.y = self.object.speed.x * math.sin(self.angle) + \ self.object.speed.y * math.cos(self.angle) self.object.speed = self.object.speed / sum(self.object.speed ** 2) ** 0.5 * abs_speed self.object.rot = glm.vec3(self.object.rot[0], self.object.rot[1], self.object.rot[2] + self.angle)
[docs] def finish(self): pass
[docs] class ChangeHeight(Maneuver): """ Class for change height maneouver """ def __init__(self, duration: int, obj: 'aircraft.Aircraft', new_height: int): super().__init__(duration, obj) self.new_height = new_height self.__previous_speed = deepcopy(obj.speed) self.speed_z = ((self.new_height - self.object.position.z) / self.duration)
[docs] def prepare(self): if isinstance(self.object.speed, glm.vec3): self.object.speed.z = self.speed_z
[docs] def finish(self): self.object.speed = deepcopy(self.__previous_speed)
[docs] class ChangeSpeed(Maneuver): """ Class for change speed maneouver """ def __init__(self, duration: int, obj: 'aircraft.Aircraft', new_speed): if not isinstance(new_speed, type(obj.speed)): raise ValueError("New speed and object speed has different type.") super().__init__(duration, obj) self.new_speed = deepcopy(new_speed) self.acceleration = deepcopy(obj.acceleration)
[docs] def prepare(self): new_acceleration = (self.new_speed - self.object.speed) / self.duration self.object.acceleration = deepcopy(new_acceleration)
[docs] def finish(self): self.object.acceleration = deepcopy(self.acceleration)