-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathentity.py
183 lines (154 loc) · 6.11 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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
import pygame
from pygame.locals import *
from vector import Vector2
from constants import *
from random import randint
import time
from singleton import Singleton
# from braindata import getSpeedFromBrainSignals
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
#Before this, load braindata file somewhere (csv/real-time, how?)
#if braindata == 0:
# print('Low Attention')
# #self.setSpeed(60)
#elif braindata == 1:
# print('Neutral Attention')
# a = 80
# #self.setSpeed(80)
#elif braindata == 2:
# print('High Attention')
# a = 100
# self.setSpeed(100)
# self.setSpeed(80) #adjusting this to a variable
# Set the initial speed based on brain signals
self.speed = 80
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
self.timer = 0
def updateSpeedFromBrainSignals(self):
# getSpeedFromBrainSignals()
# print("Speed changed to option:", brain_signal)
singleton = Singleton()
mid = singleton.baseline_value * 1e-6
low = mid-3
high = mid+3
actual_value = singleton.value * 1e-6
print("The singleton.value is :", mid)
print("Low bound set to: ", low)
print("High bound set to: ", high)
if actual_value < low:
print("low workload detected")
self.setSpeed(30)
pygame.mixer.music.load('lowAttentionMusic.mp3')
pygame.mixer.music.play(-1)
elif low < actual_value < high:
print("medium workload detected")
self.setSpeed(75)
pygame.mixer.music.load('neutralAttentionMusic.mp3')
pygame.mixer.music.play(-1)
elif actual_value >= high:
print("high workload detected")
self.setSpeed(120)
pygame.mixer.music.load('highAttentionMusic.mp3')
pygame.mixer.music.play(-1)
# time.sleep(3)
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 = 80 #reset speed also change
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)