Mercurial > games > semicongine
annotate examples/E10_pong.nim @ 145:a4e6e76128e6
add: upgrade all simple examples to new engine version
author | Sam <sam@basx.dev> |
---|---|
date | Wed, 26 Apr 2023 02:15:43 +0700 |
parents | 8bfcaed87cd6 |
children | 253dd797e719 |
rev | line source |
---|---|
74 | 1 import semicongine |
2 | |
3 const | |
77 | 4 barcolor = RGBfromHex("5A3F00").gamma(2.2) |
74 | 5 barSize = 0.1'f |
6 barWidth = 0.01'f | |
77 | 7 ballcolor = RGBfromHex("B17F08").gamma(2.2) |
74 | 8 levelRatio = 1 |
9 ballSize = 0.01'f | |
77 | 10 backgroundColor = RGBAfromHex("FAC034").gamma(2.2) |
11 ballSpeed = 60'f | |
74 | 12 |
13 var | |
145
a4e6e76128e6
add: upgrade all simple examples to new engine version
Sam <sam@basx.dev>
parents:
85
diff
changeset
|
14 level: Entity |
a4e6e76128e6
add: upgrade all simple examples to new engine version
Sam <sam@basx.dev>
parents:
85
diff
changeset
|
15 ballVelocity = newVec2f(1, 1).normalized * ballSpeed |
74 | 16 |
145
a4e6e76128e6
add: upgrade all simple examples to new engine version
Sam <sam@basx.dev>
parents:
85
diff
changeset
|
17 #[ |
74 | 18 proc globalUpdate(engine: var Engine; t, dt: float32) = |
19 var height = float32(engine.vulkan.frameSize.y) / float32( | |
20 engine.vulkan.frameSize.x) | |
21 var width = 1'f | |
22 uniforms.view.value = ortho[float32]( | |
23 0'f, width, | |
24 0'f, height, | |
25 0'f, 1'f, | |
26 ) | |
27 engine.vulkan.device.updateUniformData(pipeline, uniforms) | |
28 var player = level.firstWithName("player") | |
29 if Down in engine.input.keysDown and (player.transform.col(3).y + barSize/2) < height: | |
30 player.transform = player.transform * translate3d(0'f, 1'f * dt, 0'f) | |
31 if Up in engine.input.keysDown and (player.transform.col(3).y - barSize/2) > 0: | |
32 player.transform = player.transform * translate3d(0'f, -1'f * dt, 0'f) | |
33 | |
34 var ball = level.firstWithName("ball") | |
35 ball.transform = ball.transform * translate3d(ballVelocity[0] * dt, | |
36 ballVelocity[1] * dt, 0'f) | |
37 | |
38 # loose | |
39 if ball.transform.col(3).x - ballSize/2 <= 0: | |
77 | 40 ballVelocity = Vec2([1'f, 1'f]).normalized * ballSpeed |
41 ball.transform[0, 3] = width / 2 | |
42 ball.transform[1, 3] = height / 2 | |
74 | 43 |
44 # bounce level | |
45 if ball.transform.col(3).x + ballSize/2 > width: ballVelocity[ | |
46 0] = -ballVelocity[0] | |
47 if ball.transform.col(3).y - ballSize/2 <= 0: ballVelocity[1] = -ballVelocity[1] | |
48 if ball.transform.col(3).y + ballSize/2 > height: ballVelocity[ | |
49 1] = -ballVelocity[1] | |
50 | |
51 # bar | |
52 if ball.transform.col(3).x - ballSize/2 <= barWidth: | |
53 let | |
54 barTop = player.transform.col(3).y - barSize/2 | |
55 barBottom = player.transform.col(3).y + barSize/2 | |
56 ballTop = ball.transform.col(3).y - ballSize/2 | |
57 ballBottom = ball.transform.col(3).y + ballSize/2 | |
58 if ballTop >= barTop and ballBottom <= barBottom: | |
59 ballVelocity[0] = abs(ballVelocity[0]) | |
145
a4e6e76128e6
add: upgrade all simple examples to new engine version
Sam <sam@basx.dev>
parents:
85
diff
changeset
|
60 ]# |
74 | 61 |
62 when isMainModule: | |
145
a4e6e76128e6
add: upgrade all simple examples to new engine version
Sam <sam@basx.dev>
parents:
85
diff
changeset
|
63 var myengine = initEngine("Pong") |
a4e6e76128e6
add: upgrade all simple examples to new engine version
Sam <sam@basx.dev>
parents:
85
diff
changeset
|
64 level = newEntity("Level") |
a4e6e76128e6
add: upgrade all simple examples to new engine version
Sam <sam@basx.dev>
parents:
85
diff
changeset
|
65 var playerbarmesh = rect() |
74 | 66 playerbarmesh.vertexData.color.data = @[barcolor, barcolor, barcolor, barcolor] |
145
a4e6e76128e6
add: upgrade all simple examples to new engine version
Sam <sam@basx.dev>
parents:
85
diff
changeset
|
67 var playerbar = newEntity("playerbar", playerbarmesh) |
a4e6e76128e6
add: upgrade all simple examples to new engine version
Sam <sam@basx.dev>
parents:
85
diff
changeset
|
68 playerbar.transform = scale3d(barWidth, barSize, 1'f) * translate3d(0.5'f, 0'f, 0'f) |
a4e6e76128e6
add: upgrade all simple examples to new engine version
Sam <sam@basx.dev>
parents:
85
diff
changeset
|
69 var player = newEntity("player", playerbar) |
74 | 70 player.transform = translate3d(0'f, 0.3'f, 0'f) |
71 level.add player | |
72 | |
145
a4e6e76128e6
add: upgrade all simple examples to new engine version
Sam <sam@basx.dev>
parents:
85
diff
changeset
|
73 var ballmesh = circle() |
a4e6e76128e6
add: upgrade all simple examples to new engine version
Sam <sam@basx.dev>
parents:
85
diff
changeset
|
74 ballmesh.vertexData.color.data = newSeq[Vec3](ballmesh.vertexData.position.data.len) |
74 | 75 for i in 0 ..< ballmesh.vertexData.color.data.len: |
76 ballmesh.vertexData.color.data[i] = ballcolor | |
77 ballmesh.vertexData.transform.data = @[Unit44] | |
145
a4e6e76128e6
add: upgrade all simple examples to new engine version
Sam <sam@basx.dev>
parents:
85
diff
changeset
|
78 var ball = newEntity("ball", ballmesh) |
74 | 79 ball.transform = scale3d(ballSize, ballSize, 1'f) * translate3d(10'f, 10'f, 0'f) |
80 level.add ball | |
81 | |
77 | 82 pipeline.clearColor = backgroundColor |
74 | 83 # show something |
84 myengine.run(pipeline, globalUpdate) | |
145
a4e6e76128e6
add: upgrade all simple examples to new engine version
Sam <sam@basx.dev>
parents:
85
diff
changeset
|
85 |
a4e6e76128e6
add: upgrade all simple examples to new engine version
Sam <sam@basx.dev>
parents:
85
diff
changeset
|
86 myengine.destroy() |