-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathentity.py
127 lines (109 loc) · 4.2 KB
/
entity.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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
import pygame
from pygame.locals import *
from vector import Vector2
from constants import *
from random import randint
class Entity(object):
def __init__(self, node):
self.name = None
self.directions = {UP:Vector2(0, -1),DOWN:Vector2(0, 1),
LEFT:Vector2(-1, 0), RIGHT:Vector2(1, 0), STOP:Vector2()}
self.direction = STOP
self.setSpeed(100)
self.radius = 10
self.collideRadius = 5
self.color = WHITE
self.visible = True
self.disablePortal = False
self.goal = None
self.directionMethod = self.randomDirection
self.setStartNode(node)
self.image = None
def setPosition(self):
self.position = self.node.position.copy()
def update(self, dt):
self.position += self.directions[self.direction]*self.speed*dt
if self.overshotTarget():
self.node = self.target
directions = self.validDirections()
direction = self.directionMethod(directions)
if not self.disablePortal:
if self.node.neighbors[PORTAL] is not None:
self.node = self.node.neighbors[PORTAL]
self.target = self.getNewTarget(direction)
if self.target is not self.node:
self.direction = direction
else:
self.target = self.getNewTarget(self.direction)
self.setPosition()
def validDirection(self, direction):
if direction is not STOP:
if self.name in self.node.access[direction]:
if self.node.neighbors[direction] is not None:
return True
return False
def getNewTarget(self, direction):
if self.validDirection(direction):
return self.node.neighbors[direction]
return self.node
def overshotTarget(self):
if self.target is not None:
vec1 = self.target.position - self.node.position
vec2 = self.position - self.node.position
node2Target = vec1.magnitudeSquared()
node2Self = vec2.magnitudeSquared()
return node2Self >= node2Target
return False
def reverseDirection(self):
self.direction *= -1
temp = self.node
self.node = self.target
self.target = temp
def oppositeDirection(self, direction):
if direction is not STOP:
if direction == self.direction * -1:
return True
return False
def validDirections(self):
directions = []
for key in [UP, DOWN, LEFT, RIGHT]:
if self.validDirection(key):
if key != self.direction * -1:
directions.append(key)
if len(directions) == 0:
directions.append(self.direction * -1)
return directions
def randomDirection(self, directions):
return directions[randint(0, len(directions)-1)]
def goalDirection(self, directions):
distances = []
for direction in directions:
vec = self.node.position + self.directions[direction]*TILEWIDTH - self.goal
distances.append(vec.magnitudeSquared())
index = distances.index(min(distances))
return directions[index]
def setStartNode(self, node):
self.node = node
self.startNode = node
self.target = node
self.setPosition()
def setBetweenNodes(self, direction):
if self.node.neighbors[direction] is not None:
self.target = self.node.neighbors[direction]
self.position = (self.node.position + self.target.position) / 2.0
def reset(self):
self.setStartNode(self.startNode)
self.direction = STOP
self.speed = 100
self.visible = True
def setSpeed(self, speed):
self.speed = speed * TILEWIDTH / 16
def render(self, screen):
if self.visible:
if self.image is not None:
adjust = Vector2(TILEWIDTH, TILEHEIGHT) / 2
p = self.position - adjust
screen.blit(self.image, p.asTuple())
else:
p = self.position.asInt()
pygame.draw.circle(screen, self.color, p, self.radius)