-
Notifications
You must be signed in to change notification settings - Fork 0
/
object.py
242 lines (188 loc) · 7.03 KB
/
object.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
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
import animation
from util import Rect, Vector
class AbstractObject(object):
"""All game world objects are ultimately derived from this class.
Contains a variety of useful functions used for movement and
collision detection"""
def __init__(self, pos, shape, team=1, moveSpeed = 50.0, moveDir=None):
#General
self.collisionType = None
#All objects are alive/enabled by default.
self.alive = True
#Objects position (the center)
self.pos = Vector(pos)
#The objects shape, used for collision detection
self.shape = shape
self.moveSpeed = moveSpeed
if not moveDir:
self.moveDir = Vector(0,0)
else:
self.moveDir = Vector(moveDir)
#Velocity is the normalized direction scaled by the speed
#TODO: calculate on update?
if not self.moveDir.isNullVector():
#self.moveDir.normalize()
self.vel = self.moveDir.getNormalized() * self.moveSpeed
else:
self.vel = Vector(0,0)
#Keep track of where the objects wants to go next
self.requestedPos = pos
#Keep track of which direction the object is 'facing'
self.orientation = None
#Set up the animation object
self.frames = []
#TODO: this is temporary for testing
anim = animation.TestAnimation(size = shape.getSize())
self.animationPlayer = animation.AnimationPlayer(anim, 1.0, True)
#set the team property
self.team = team
#Turning
self.turning = False
self.turnTime = 0.05
self.turnTimer = 0
def onCollide(self, object):
"""Handles collision"""
pass
#print object
#if object.collisionType == 'PROJECTILE':
# print 'proj'
def orientationToString(self):
#converts the orientation to a string
#makes it easier for animation
#if self.orientation == None: #FIX FOR IF THERE IS NO ORIENTATION
# return 'north'
string = ''
if self.orientation[0] == 0 and self.orientation[1] < 0:
string = 'UP'
elif self.orientation[0] == 0 and self.orientation[1] > 0:
string = 'DOWN'
elif self.orientation[0] > 0 and self.orientation[1] == 0:
string = 'RIGHT'
elif self.orientation[0] < 0 and self.orientation[1] == 0:
string = 'LEFT'
elif self.orientation[0] < 0 and self.orientation[1] < 0:
string = 'UPLEFT'
elif self.orientation[0] > 0 and self.orientation[1] > 0:
string = 'DOWNRIGHT'
elif self.orientation[0] < 0 and self.orientation[1] > 0:
string = 'DOWNLEFT'
elif self.orientation[0] > 0 and self.orientation[1] < 0:
string = 'UPRIGHT'
return string
def draw(self, screen, offset):
#draw the animation, centered at the objects position
# and offset according to the location of the camera
self.animationPlayer.draw(screen, self.getPos()+offset)
def setPos(self, pos):
self.pos = Vector(pos)
def getPos(self):
return self.pos
def getPosInTime(self, time):
"""Calculate the objects position in 'time' as a function
of its current position and velocity."""
return self.pos + self.vel*time
#
# def getRect(self):
# #return (self.pos[0],self.pos[1],
# # self.size[0],self.size[1])
#
# return Rect(self.pos, self.size)
def getRequestedPos(self):
return self.requestedPos
# def getVelocity(self):
# return self.moveSpeed
# def isCollision(self, o,requested=False):
# #request=True means we check requested positions
# """Check if this object's rect overlaps the other's rect"""
#
# if requested:
# ax1,ay1 = self.requestedPos
# else:
# ax1,ay1 = self.pos
# ax2,ay2 = ax1+self.size[0],ay1+self.size[1]
#
# if requested:
# bx1,by1 = o.requestedPos
# else:
# bx1,by1 = o.pos
# bx2,by2 = bx1+o.size[0],by1+o.size[1]
#
# if ax1 < bx2 and ax2 > bx1 and ay1 < by2 and ay2 > by1:
# return True
# else:
# return False
def requestMove(self, direction):
"""Indicate when preferred direction this object wants to go"""
assert direction is not None
if direction != (0, 0):
normalizedOrientation = self.orientation.getNormalized()
normalizedDirection = Vector(direction).getNormalized()
if normalizedOrientation == normalizedDirection:
#only move if facing the right direction, else turn
#update the direction
self.moveDir = Vector(direction)
self.vel = self.moveDir.getNormalized()*self.moveSpeed
self.turning = False
#update the objects velocity
#TODO: what if the move direction is 0 vector?
#if not self.moveDir.isNullVector():
#self.moveDir.normalize()
#self.vel = self.moveDir.getNormalized()*self.moveSpeed
#else:
#self.vel = Vector(0,0)
else:
self.turning = True
self.vel = Vector(0,0)
self.turn(Vector(direction))
else:
#not trying to move
self.vel = Vector(0,0)
self.turning = False
def turn(self, direction):
"""Use cross product to find the best direction and turns 45 degrees"""
if self.orientation.getNormalized() != direction.getNormalized(): #prevents over turning incase time is large
if self.turnTimer >= self.turnTime:
o = self.orientation.getNormalized()
d = direction.getNormalized()
value = o[0]*d[1] - o[1]*d[0]
sinCos = 0.707106781 #rotate 45 degrees so sin and cos are equal
if value >= 0:
x = self.orientation[0]*sinCos - self.orientation[1]*sinCos
y = self.orientation[0]*sinCos + self.orientation[1]*sinCos
else:
x = self.orientation[0]*sinCos + self.orientation[1]*sinCos
y = self.orientation[1]*sinCos - self.orientation[0]*sinCos
#Smooths out all rounding errors
if x > 0:
x = 1
elif x < 0:
x = -1
if y > 0:
y = 1
elif y < 0:
y = -1
self.orientation = Vector(x,y)
self.turnTimer -= self.turnTime
self.turn(direction) #turns again if time exceeds one turn, but dont want to overturn
def updatePos(self, time):
"""Set the objects position equal to its requested position. Usually
done after collision resolution"""
self.pos = self.requestedPos
def updateRequestedPos(self, time):
"""Update the objects requested position, done before
collision resolution"""
self.requestedPos = self.getPosInTime(time)
def update(self, time):
#TODO: animations should be updated after collision detection
#update the animation
if self.animationPlayer:
self.animationPlayer.update(time)
#update turning clock
if self.turning:
self.turnTimer += time
class TestWalker(AbstractObject):
def __init__(self, pos, **kwargs):
shape = Rect.createAtOrigin(64, 64)
AbstractObject.__init__(self, pos, shape, **kwargs)
self.collisionType = 'UNIT'
self.orientation = Vector(1,0)