-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmy_player.py
86 lines (67 loc) · 2.35 KB
/
my_player.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
import math
import numpy as np
class My_player:
"""Representation of player in the game"""
def __init__(self, x, y) -> None:
self.x = x
self.y = y
self.last_x = x
self.last_y = y
self.rotate = 0
self.direction = self.get_self_orientation()
def update(self, x: float, y: float) -> None:
"""Update position of player, last position and direction
Args:
x (float): new X position
y (float): new Y position
"""
self.last_x = self.x
self.last_y = self.y
self.x = x
self.y = y
self.direction = self.get_self_orientation()
def get_position(self) -> np.array:
"""Return position of player
Returns:
np.array: [x, y] position of player
"""
return np.array([self.x, self.y])
def get_last_position(self) -> np.array:
"""Return last position of player
Returns:
np.array: [x, y] last position of player
"""
return np.array([self.last_x, self.last_y])
def diff_angle(self, new_angle: float) -> float:
"""Compare the orientation of the player to a given angle (mostly target orientation from world's perspective)
Args:
new_angle (float): new angle to compare
Returns:
float: angle
"""
angle = new_angle - self.get_self_orientation()
self.rotate = angle
return angle
def get_self_orientation(self) -> float:
"""Get angle from the vector player and player_last_position
Returns:
float: angle
"""
x, y = self.get_position() - self.get_last_position()
return round(math.degrees(math.atan2(y, x)), 10) % 360.0
def get_target_orientation(self, target: np.array) -> float:
"""Get angle from the vector player-target and the world
Returns:
float: angle
"""
x, y = target - self.get_position()
return round(math.degrees(math.atan2(y, x)), 10) % 360.0
def __str__(self) -> str:
"""Print attributes of class for debugging
Returns:
str: string printed when call `print()` on class object
"""
string = ""
for key, value in self.__dict__.items():
string = string + f"{key}={value}\n"
return string