-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGameWorld.py
114 lines (89 loc) · 2.96 KB
/
GameWorld.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
from Vector import Vec2d, ex
import Robots
import AI.ExampleAI
import AI.DumbAI
import random
import math
import cairo
# The world size
worldsize = [600 * 5, 600 * 5]
# The window size.
windowsize = [600, 600]
# Location of top left corner of window
windowloc = [0, 0]
# Scale factor - less than 1 means zoom out
scale = 1
invscale = 1.0/scale
scalingFixedPoint = (0, 0)
sprites = []
zoomFactor = 1.1
zoomMatrix = cairo.Matrix(xx = 1.0, yx = 0.0, xy = 0.0, yy = 1.0, x0 = 0.0, y0 = 0.0)
def setScale(s):
global scale, invscale
scale = float(s)
invscale = 1.0/s
def multiplyScaleBy(m):
global scale, invscale
scale *= float(m)
invscale = 1.0/scale
def totalZoom():
xx, yx, xy, yy, x0, y0 = zoomMatrix
return abs(xx)
def zoomIn(x):
global zoomFactor, windowloc, windowsize, scalingFixedPoint, zoomMatrix, scale, invscale
# The more zoomed in we are (the higher the scale), the less we should zoom)
realZoomFactor = 1 + 0.1*zoomFactor/scale
multiplyScaleBy(realZoomFactor)
newZoomMatrix = cairo.Matrix()
newZoomMatrix.translate(x[0], x[1])
newZoomMatrix.scale(realZoomFactor, realZoomFactor)
newZoomMatrix.translate(-x[0], -x[1])
zoomMatrix = zoomMatrix.multiply(newZoomMatrix)
def zoomOut(x):
global zoomFactor, scalingFixedPoint, scale, invscale, zoomMatrix
realZoomFactor = 1.0/1.1 #1.0/zoomFactor
multiplyScaleBy(realZoomFactor)
newZoomMatrix = cairo.Matrix()
newZoomMatrix.translate(x[0], x[1])
newZoomMatrix.scale(realZoomFactor, realZoomFactor)
newZoomMatrix.translate(-x[0], -x[1])
zoomMatrix = zoomMatrix.multiply(newZoomMatrix)
def windowToWorldTransform():
global zoomMatrix, windowloc
t = cairo.Matrix()
t = t.multiply(zoomMatrix)
tmp = cairo.Matrix()
tmp.translate(-windowloc[0], -windowloc[1])
t = tmp.multiply(t)
return t
def worldToWindowTransform():
t = windowToWorldTransform()
t.invert()
return t
def worldCoordsToWindowCoords(cr, x):
# Find world centre in window coords
matrix = cr.get_matrix()
cr.set_matrix(windowToWorldTransform())
wc = cr.user_to_device( x[0], x[1] )
cr.set_matrix(matrix)
return wc
def moveWindowTowardsWorldCoords(cr, x):
x = worldCoordsToWindowCoords(cr, x)
global windowloc, windowsize
windowcentre = (windowsize[0]/2, windowsize[1]/2)
dx = (x[0] - windowcentre[0], x[1] - windowcentre[1])
windowloc[0] += 0.02*dx[0]
windowloc[1] += 0.02*dx[1]
def getSpawnPoint(colour=0):
# Spawn locations
spawn = [ Vec2d(worldsize[0]/5, worldsize[1]/5), Vec2d(worldsize[0]/5, 4*worldsize[1]/5), Vec2d(4*worldsize[0]/5, worldsize[1]/5), Vec2d(4*worldsize[0]/5, 4*worldsize[1]/5) ]
return spawn[ colour%4 ] + Vec2d( (1-2*random.random())*worldsize[0]/5, (1-2*random.random())*worldsize[1]/5)
def addBots(num, col=Robots.Teams.RED, ai=AI.ExampleAI.AI()):
maxspeed = 2
hp = 100
uids = []
for i in range(num):
r = Robots.Robot(col, getSpawnPoint(col), maxspeed, ex.rotate(random.random()*2*math.pi), hp, ai)
sprites.append(r)
uids.append(r.UID)
return uids