Skip to content

Commit 89d6286

Browse files
committed
Created die function which, removes the agent from the schedule and the grid. towardsxy function, calculates angle between a given coordinate and horizon as if the current position is the origin. towards function, calculates angle between an agent and horizon as if the current position is the origin. facexy function, makes agent face a given coordinate. face function, makes agent face another agent
1 parent a6fd559 commit 89d6286

File tree

1 file changed

+32
-0
lines changed

1 file changed

+32
-0
lines changed

mesa/agent.py

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,38 @@ def distance(self, another_agent):
7979
"""Gives you the distance between the agent and another agent"""
8080
return self.distancexy(another_agent.pos[0], another_agent.pos[1])
8181

82+
def die(self):
83+
"""Removes the agent from the schedule and the grid """
84+
try:
85+
self.model.schedule.remove(self)
86+
except:
87+
print("agent.py (die): could not remove agent from self.model.schedule")
88+
try:
89+
self.model.space.remove_agent(self)
90+
except:
91+
print("agent.py (die): could not remove agent from self.model.space")
92+
93+
def towardsxy(self, x, y):
94+
"""Calculates angle between a given coordinate and horizon as if the current position is the origin"""
95+
dx = x - float(self.pos[0])
96+
dy = float(self.pos[1]) - y
97+
if dx == 0:
98+
return 90 if dy > 0 else 270
99+
else:
100+
return math.degrees(math.atan2(dy, dx))
101+
102+
def towards(self, another_agent):
103+
"""Calculates angle between an agent and horizon as if the current position is the origin"""
104+
return self.towardsxy(*another_agent.pos)
105+
106+
def facexy(self, x, y):
107+
"""Makes agent face a given coordinate"""
108+
self.heading = self.towardsxy(x, y)
109+
110+
def face(self, another_agent):
111+
"""Makes agent face another agent"""
112+
x, y = another_agent.pos
113+
self.facexy(x, y)
82114

83115
@property
84116
def random(self) -> Random:

0 commit comments

Comments
 (0)