-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTechDemo2.py
266 lines (217 loc) · 11.8 KB
/
TechDemo2.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
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
from direct.showbase.ShowBase import ShowBase
from panda3d.core import CollisionTraverser, CollisionNode
from panda3d.core import CollisionHandlerQueue, CollisionRay
from panda3d.core import AmbientLight, DirectionalLight, LightAttrib
from panda3d.core import LPoint3, LVector3, BitMask32
from direct.gui.OnscreenText import OnscreenText
from direct.showbase.DirectObject import DirectObject
from direct.task.Task import Task
from panda3d.core import Lens, OrthographicLens
import sys
def color(r,g,b): # changes a rgb code into a range between 0 and 1 values
return((r/255,g/255,b/255))
def PointAtZ(z, point, vec): # Not my function
return point + vec * ((z - point.getZ()) / vec.getZ())
# Checks whether two vectors are close enough
def checkPos(p1, p2):
x1 = p1.getX()
x2 = p2.getX()
y1 = p1.getY()
y2 = p2.getY()
# z1 = p1.getZ()
# z2 = p2.getZ()
return abs(x1-x2) <= 0.5 and abs(y1-y2) <= 0.5
# Main class
class MouseTask(ShowBase):
def __init__(self):
super().__init__() # Inheriting all the attributes of Showbase class
self.disableMouse()
self.squares = [] # Storing the models of square
self.pieces = [] # Storing the models of pieces
count = 0
for index in range(1, 65): # 1 chess board
self.squares += [index]
self.pieces += [index]
index = -1
count= 1
for x in range(-3,5): # Random range of 8 unit gap
# going to next row
for y in range(13,5,-1):# Random range of 8 unit gap
index += 1
# going through a next column
if count % 2 == 0:
self.squares[index] = loader.loadModel("models/square.egg")
# Loading the egg file in a variable
self.squares[index].reparentTo(render)
# Adding to parent node
self.squares[index].setPos(x,y,0)
# Setting the location
self.squares[index].setColor(0,0,0)
# Black color code
count+= 1
else:
self.squares[index] = loader.loadModel("models/square.egg")
self.squares[index].reparentTo(render)
self.squares[index].setPos(x,y,0)
self.squares[index].setColor(1, 1, 1)
count+= 1
count+= 1
index = -1
count= 1
for x in range(-3,5): # 32 Pieces in 64 squares
# going to next row
for y in range(13,5,-1):
index += 1
# going throug a constant row
# PAWN
if y == 12:
self.pieces[index] = loader.loadModel("models/pawn.egg")
self.pieces[index].reparentTo(render)
self.pieces[index].setPos(x,y,0)
self.pieces[index].setColor(1,1,1)
elif y == 7:
self.pieces[index] = loader.loadModel("models/pawn.egg")
self.pieces[index].reparentTo(render)
self.pieces[index].setPos(x,y,0)
self.pieces[index].setColor(color(150, 75, 0)[0],
color(150, 75, 0)[1],color(150, 75, 0)[2]) # Brown color
# Rook
if (x, y) in [(-3, 13), (4,13), (-3,6), (4,6)]:
if y == 13:
self.pieces[index] = loader.loadModel("models/rook.egg")
self.pieces[index].reparentTo(render)
self.pieces[index].setPos(x,y,0)
self.pieces[index].setColor(1,1,1)
elif y == 6:
self.pieces[index] = loader.loadModel("models/rook.egg")
self.pieces[index].reparentTo(render)
self.pieces[index].setPos(x,y,0)
self.pieces[index].setColor(color(150, 75, 0)[0],
color(150, 75, 0)[1],color(150, 75, 0)[2]) # Brown color
# Knight
if (x, y) in [(-2, 13), (3,13), (-2,6), (3,6)]:
if y == 13:
self.pieces[index] = loader.loadModel("models/knight.egg")
self.pieces[index].reparentTo(render)
self.pieces[index].setPos(x,y,0)
self.pieces[index].setColor(1,1,1)
elif y == 6:
self.pieces[index] = loader.loadModel("models/knight.egg")
self.pieces[index].reparentTo(render)
self.pieces[index].setPos(x,y,0)
self.pieces[index].setColor(color(150, 75, 0)[0],
color(150, 75, 0)[1],color(150, 75, 0)[2]) # Brown color
# Bishop
if (x, y) in [(-1, 13), (2,13), (-1,6), (2,6)]:
if y == 13:
self.pieces[index] = loader.loadModel("models/bishop.egg")
self.pieces[index].reparentTo(render)
self.pieces[index].setPos(x,y,0)
self.pieces[index].setColor(1,1,1)
elif y == 6:
self.pieces[index] = loader.loadModel("models/bishop.egg")
self.pieces[index].reparentTo(render)
self.pieces[index].setPos(x,y,0)
self.pieces[index].setColor(color(150, 75, 0)[0],
color(150, 75, 0)[1],color(150, 75, 0)[2])
# KIng
if (x, y) in [(0, 13), (0,6)]:
if y == 13:
self.pieces[index] = loader.loadModel("models/king.egg")
self.pieces[index].reparentTo(render)
self.pieces[index].setPos(x,y,0)
self.pieces[index].setColor(1,1,1)
elif y == 6:
self.pieces[index] = loader.loadModel("models/king.egg")
self.pieces[index].reparentTo(render)
self.pieces[index].setPos(x,y,0)
self.pieces[index].setColor(color(150, 75, 0)[0],
color(150, 75, 0)[1],color(150, 75, 0)[2]) # Brown color
# Queen
if (x, y) in [(1, 13), (1,6)]:
if y == 13:
self.pieces[index] = loader.loadModel("models/queen.egg")
self.pieces[index].reparentTo(render)
self.pieces[index].setPos(x,y,0)
self.pieces[index].setColor(1,1,1)
elif y == 6:
self.pieces[index] = loader.loadModel("models/queen.egg")
self.pieces[index].reparentTo(render)
self.pieces[index].setPos(x,y,0)
self.pieces[index].setColor(color(150, 75, 0)[0],
color(150, 75, 0)[1],color(150, 75, 0)[2])
count+= 1
count+= 1
self.pickerRay = CollisionRay() # Instance of the collision ray class
self.setBackgroundColor(color(0,0,125)[0],
color(0,125,0)[1],color(0,0,125)[2]) # Any
self.camera.setPos(0.5,-5,10)
# 0.5 along x and -5 along y and 10 along z
self.camera.setHpr(0,-30,0)
# Heading 0 degree, pitch -30 and roll 0 degree
self.setupLights()
self.accept("mouse1", self.mousePressed) # Only left click
self.taskMgr.add(self.update, "update")# Calls the self.update funtion each frame
self.pieceSelected = None
self.piece = None
def mousePressed(self):
if self.pieceSelected == None: # 1st time left click
if self.mouseWatcherNode.hasMouse():
# get the mouse position
mpos = self.mouseWatcherNode.getMouse()
# Set the position of the ray based on the mouse position
self.pickerRay.setFromLens(self.camNode, mpos.getX(), mpos.getY())
pointOfRay = render.getRelativePoint(camera, self.pickerRay.getOrigin())
# Same thing with the direction of the ray
vectorOfRay = render.getRelativeVector(
camera, self.pickerRay.getDirection())
point = PointAtZ(0.01, pointOfRay, vectorOfRay)
for index in range(64):
if not isinstance(self.pieces[index], int):
# Non objects are filled with integers; requires changes
if checkPos(self.pieces[index], point):
# Checks for the identical positions
self.piece = self.pieces[index] # Storing that piece
self.pieceSelected = "Yes"
elif self.pieceSelected != None: # 2nd time left click
if self.mouseWatcherNode.hasMouse():
# get the mouse position
mpos = self.mouseWatcherNode.getMouse()
# Set the position of the ray based on the mouse position
self.pickerRay.setFromLens(self.camNode, mpos.getX(), mpos.getY())
pointOfRay = render.getRelativePoint(camera, self.pickerRay.getOrigin())
# Same thing with the direction of the ray
vectorOfRay = render.getRelativeVector(
camera, self.pickerRay.getDirection())
point = PointAtZ(0.01, pointOfRay, vectorOfRay)
# Converts the vectors and points into readable coordinates
for index in range(64):
posOfSq = self.squares[index].getPos()
if checkPos(point, posOfSq):
self.piece.setPos(posOfSq) # placing the piece
self.piece = None
self.pieceSelected = None
def setupLights(self): # Not my function
ambientLight = AmbientLight("ambientLight")
ambientLight.setColor((.8, .8, .8, 1))
directionalLight = DirectionalLight("directionalLight")
directionalLight.setDirection(LVector3(0, 45, -45))
directionalLight.setColor((0.2, 0.2, 0.2, 1))
render.setLight(render.attachNewNode(directionalLight))
render.setLight(render.attachNewNode(ambientLight))
def update(self, task): # moves the piece along with the pointer
if self.piece != None:
if self.mouseWatcherNode.hasMouse():
# get the mouse position
mpos = self.mouseWatcherNode.getMouse()
# Set the position of the ray based on the mouse position
self.pickerRay.setFromLens(self.camNode, mpos.getX(), mpos.getY())
pointOfRay = render.getRelativePoint(camera, self.pickerRay.getOrigin())
# Same thing with the direction of the ray
vectorOfRay = render.getRelativeVector(
camera, self.pickerRay.getDirection())
point = PointAtZ(0.01, pointOfRay, vectorOfRay)
self.piece.setPos(point) # Moving the piece
return Task.cont # For infinite call
game = MouseTask()
game.run()