@@ -48,6 +48,7 @@ def __init__(self, unique_id: int, model: Model) -> None:
4848 self .unique_id = unique_id
4949 self .model = model
5050 self .pos : Position | None = None
51+ self .heading = 90
5152
5253 # register agent
5354 try :
@@ -75,6 +76,81 @@ def step(self) -> None:
7576 def advance (self ) -> None :
7677 pass
7778
79+ def move_forward_or_backward (self , amount , sign ):
80+ """Does the calculation to find the agent's next move and is used within the forward and backward functions"""
81+ new_x = float (self .pos [0 ]) + sign * math .cos (self .heading * math .pi / 180 ) * amount
82+ new_y = float (self .pos [1 ]) + sign * math .sin (self .heading * math .pi / - 180 ) * amount
83+ next_pos = (new_x , new_y )
84+ try :
85+ self .model .space .move_agent (self , next_pos )
86+ except :
87+ print ("agent.py (forward_backwards): could not move agent within self.model.space" )
88+
89+ def move_forward (self , amount ):
90+ """Moves the agent forward by the amount given"""
91+ self .move_forward_or_backward (amount , 1 )
92+
93+ def move_backward (self , amount ):
94+ """Moves the agent backwards from where its facing by the given amount"""
95+ self .move_forward_or_backward (amount , - 1 )
96+
97+ def turn_right (self , degree ):
98+ """Turns the agent right by the given degree"""
99+ self .heading = (self .heading - degree ) % 360
100+
101+ def turn_left (self , degree ):
102+ """Turns the agent left by the given degree"""
103+ self .heading = (self .heading + degree ) % 360
104+
105+ def setxy (self , x , y ):
106+ """Sets the current position to the specified x,y parameters"""
107+ self .pos = (x , y )
108+
109+ def set_pos (self , apos ):
110+ """Sets the current position to the specified pos parameter"""
111+ self .pos = apos
112+
113+ def distancexy (self , x , y ):
114+ """Gives you the distance of the agent and the given coordinate"""
115+ return math .dist (self .pos , (x , y ))
116+
117+ def distance (self , another_agent ):
118+ """Gives you the distance between the agent and another agent"""
119+ return self .distancexy (another_agent .pos [0 ], another_agent .pos [1 ])
120+
121+ def die (self ):
122+ """Removes the agent from the schedule and the grid """
123+ try :
124+ self .model .schedule .remove (self )
125+ except :
126+ print ("agent.py (die): could not remove agent from self.model.schedule" )
127+ try :
128+ self .model .space .remove_agent (self )
129+ except :
130+ print ("agent.py (die): could not remove agent from self.model.space" )
131+
132+ def towardsxy (self , x , y ):
133+ """Calculates angle between a given coordinate and horizon as if the current position is the origin"""
134+ dx = x - float (self .pos [0 ])
135+ dy = float (self .pos [1 ]) - y
136+ if dx == 0 :
137+ return 90 if dy > 0 else 270
138+ else :
139+ return math .degrees (math .atan2 (dy , dx ))
140+
141+ def towards (self , another_agent ):
142+ """Calculates angle between an agent and horizon as if the current position is the origin"""
143+ return self .towardsxy (* another_agent .pos )
144+
145+ def facexy (self , x , y ):
146+ """Makes agent face a given coordinate"""
147+ self .heading = self .towardsxy (x , y )
148+
149+ def face (self , another_agent ):
150+ """Makes agent face another agent"""
151+ x , y = another_agent .pos
152+ self .facexy (x , y )
153+
78154 @property
79155 def random (self ) -> Random :
80156 return self .model .random
0 commit comments