-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathphysical_object.py
118 lines (92 loc) · 2.95 KB
/
physical_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
#!/usr/bin/env python
import pygame
import constants
import play_sound
from pygame import mixer
from pygame.mixer import Sound
COLLISION_TYPE_UNDEF = 0
COLLISION_TYPE_GROUND = 1
COLLISION_TYPE_BULLET = 2
COLLISION_TYPE_SHIP = 3
TARGET_TYPE_UNDEF = 0
TARGET_TYPE_SHIP = 1
OWNER_NONE = 0
OWNER_ATTACKER = OWNER_NONE + 1
OWNER_DEFENDER = OWNER_ATTACKER + 1
NO_OBJECT_ID = -1
INITIAL_OBJECT_ID_INDEX = 0
# note: it's not necessary for the client to call this function, though it
# would be nice
def init(initializingOnServer):
PhysicalObject.isOnServer = initializingOnServer
#if PhysicalObject.isOnServer:
# print "PhysicalObjects are now being constructed for the server."
#else:
# print "PhysicalObjects are now being constructed for the client."
class PhysicalObject(pygame.sprite.Sprite):
"""This class represents a generic physical object"""
# class variables
isOnServer = False
nextObjectIDIndex = INITIAL_OBJECT_ID_INDEX
# instance variables
childObjects = [] # temporary storage for physical objects created by this object (such as bullets)
networkID = None
controllingPlayer = OWNER_NONE
# more instance variables
objectID = NO_OBJECT_ID
area = pygame.rect.Rect(0, 0, 0, 0)
rect = pygame.rect.Rect(0, 0, 0, 0)
physicsRect = pygame.rect.Rect(0, 0, 0, 0)
collisionType = COLLISION_TYPE_UNDEF
targetType = TARGET_TYPE_UNDEF
destroyed = False
r_x = 0.0 # location x; I didn't want to use something as generic as "x" and "y", so I pulled from physics/engineering convention
r_y = 0.0 # location y
v_x = 0.0 # velocity x
v_y = 0.0 # velocity y
LEFT = False
RIGHT = False
UP = False
DOWN = False
def __init__(self, position):
pygame.sprite.Sprite.__init__(self)
self.setX(position[0])
self.setY(position[1])
if PhysicalObject.isOnServer:
#print "On server"
self.objectID = 2*PhysicalObject.nextObjectIDIndex # even IDs
else:
#print "On client"
self.objectID = 2*PhysicalObject.nextObjectIDIndex + 1 # odd IDs
PhysicalObject.nextObjectIDIndex += 1
#print "Constructing object with object ID ", self.objectID
def step(self, scrollPosition):
#update position
self.setX(self.getX() + self.v_x)
self.setY(self.getY() + self.v_y)
def resolveCollisionWith(self, otherObject):
# apply collision effects to this object, not the other object
if otherObject.collisionType == COLLISION_TYPE_BULLET and otherObject.controllingPlayer != self.controllingPlayer:
self.destroyed = True
explode = pygame.mixer.Sound(constants.EXPLOSION_SFX)
explode.set_volume(0.4)
play_sound.PlaySounds(explode, 2)
def getX(self):
return self.r_x
def setX(self, newX):
self.r_x = newX
self.rect.x = self.r_x
self.physicsRect.x = self.r_x
def getY(self):
return self.r_y
def setY(self, newY):
self.r_y = newY
self.rect.y = self.r_y
self.physicsRect.y = self.r_y
def getVX(self):
return self.v_x
def getVY(self):
return self.v_y
def setPosition(self, newX, newY):
self.setX(self, newX)
self.setY(self, newY)