/
jump_rotate.cxs
238 lines (159 loc) · 5.61 KB
/
jump_rotate.cxs
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
' -----------------------------------------------------------------------------
' Rotate background around player...
' -----------------------------------------------------------------------------
' Demo 2. Quick hack showing rotation while jumping. Worst jumping code ever,
' hard-coded rotation speed for jump height, and very buggy... but that's not
' the point! It rotates correctly around the player, that's all.
' Move using LEFT/RIGHT and hit SPACE to jump really badly...
' -----------------------------------------------------------------------------
' Basic imports for most purposes...
' -----------------------------------------------------------------------------
Import mojo
' -----------------------------------------------------------------------------
' DISPLAY ROTATION FUNCTIONS...
' -----------------------------------------------------------------------------
' -----------------------------------------------------------------------------
' RotateDisplay: rotates view around x, y by angle...
' -----------------------------------------------------------------------------
Function RotateDisplay (X:Float, Y:Float, angle:Float)
PushMatrix ' Store current rotation, scale, etc
Translate X, Y ' Shift origin across to here
Rotate angle ' Rotate around origin
Translate -X, -Y ' Shift origin back where it was
End
' -----------------------------------------------------------------------------
' ResetDisplay: resets view back to normal...
' -----------------------------------------------------------------------------
Function ResetDisplay ()
PopMatrix ' Restore rotation, scale, etc
End
' -----------------------------------------------------------------------------
' USAGE...
' -----------------------------------------------------------------------------
' Step 1) In OnRender, call RotateDisplay with x/y position to rotate around
' and the angle at which to draw...
' Step 2) DRAW BACKGROUND STUFF HERE!
' Step 3) Call ResetDisplay and continue drawing as normal...
' See OnRender code for example...
' -----------------------------------------------------------------------------
' TOP TIP! You can use this call to just rotate around screen centre...
' -----------------------------------------------------------------------------
' RotateDisplay DeviceWidth * 0.5, DeviceHeight * 0.5, ang
Class Block
Field X:Float
Field Y:Float
Field Image:Image
End
' App class...
Class Game Extends App
Field gravity:Float = 0.05
Field player:Image
Field block:Image
Field px:Float
Field py:Float
Field pxs:Float
Field pys:Float
Field groundy:Float
Field ang:Float
Field angspeed:Float
Field rotating:Bool
Field blocklist:List <Block>
Method OnCreate () ' Load media and SET UPDATE RATE here...
' px = DeviceWidth * 0.5
py = DeviceHeight * 0.5
player = LoadCenteredImage ("player.png")
block = LoadCenteredImage ("block.png")
blocklist = New List <Block>
For Local loop:Int = 1 To 100
Local temp:Block = New Block
temp.Image = block
temp.X = Rnd (-DeviceWidth, DeviceWidth)
temp.Y = Rnd (-DeviceHeight, DeviceHeight)
blocklist.AddLast temp
Next
groundy = 300 + (player.Height * 0.5) * 0.25
SetUpdateRate 60
End
Method OnUpdate () ' Take input and update game here...
Local movinglr:Bool
If Not rotating
If KeyDown (KEY_LEFT)
pxs = pxs - 0.1
movinglr = True
Endif
If KeyDown (KEY_RIGHT)
pxs = pxs + 0.1
movinglr = True
Endif
If py >= 295
If KeyDown (KEY_SPACE)
pys = -3
rotating = True
Endif
Endif
Endif
If pxs > 2 Then pxs = 2
If pxs < -2 Then pxs = -2
px = px + pxs
If Not movinglr And py >= 295 Then pxs = pxs * 0.99
pys = pys + gravity
py = py + pys
If py > 300
py = 300
pys = -pys * 0.5
Endif
If px < 0 Or px > DeviceWidth
pxs = -pxs
px = px + pxs
Endif
If py < 0 Or py > DeviceHeight
pys = -pys
py = py + pys
Endif
If rotating
ang = ang + 3 * Sgn (pxs)
If ang < -360 Or ang > 360
ang = 0
rotating = False
Endif
Endif
End
Method OnRender () ' Draw frame here...
Cls 64, 96, 128
' ---------------------------------------------------------------------
' Rotate display around player position...
' ---------------------------------------------------------------------
RotateDisplay px, py, ang
' ---------------------------------------------------------------------
' DRAW BACKGROUND...
SetColor 0, 64, 0
DrawRect -DeviceWidth, groundy, DeviceWidth * 4.0, DeviceHeight * 3.0 ' Just making sure...
SetColor 255,255,255
For Local B:Block = Eachin blocklist
DrawImage B.Image, B.X, B.Y, 0, 0.25, 0.25
Next
DrawText "-11 -10 -09 -08 -07 -06 -05 -04 -03 -02 -01 000 +01 +02 +03 +04 +05 +06 +07 +08 +09 +10 + 11", 0, groundy
' ---------------------------------------------------------------------
' Set display back to previous (ie. normal) state...
' ---------------------------------------------------------------------
ResetDisplay
' ---------------------------------------------------------------------
' DRAW PLAYER AT NORMAL ORIENTATION and DRAW DISPLAY TEXT...
DrawImage player, px, py, 0, 0.25, 0.25
DrawText "[CURSORS - move player] [SPACE - jump]", 20, 20
End
End
' Main function...
Function Main ()
New Game
End
' Set image's handle to centre...
Function MidHandle (Image:Image)
Image.SetHandle Image.Width () * 0.5, Image.Height () * 0.5
End
' Load an image with handle already centered (uses MidHandle function above)...
Function LoadCenteredImage:Image (Image:String)
Local img:Image = LoadImage (Image)
MidHandle img
Return img
End