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
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
74
779607656b12 fix: stuff, add working pong
Sam <sam@basx.dev>
parents:
diff changeset
1 import semicongine
779607656b12 fix: stuff, add working pong
Sam <sam@basx.dev>
parents:
diff changeset
2
779607656b12 fix: stuff, add working pong
Sam <sam@basx.dev>
parents:
diff changeset
3 const
77
4b16d2af316a add: color functions + gamma correction
Sam <sam@basx.dev>
parents: 74
diff changeset
4 barcolor = RGBfromHex("5A3F00").gamma(2.2)
74
779607656b12 fix: stuff, add working pong
Sam <sam@basx.dev>
parents:
diff changeset
5 barSize = 0.1'f
779607656b12 fix: stuff, add working pong
Sam <sam@basx.dev>
parents:
diff changeset
6 barWidth = 0.01'f
77
4b16d2af316a add: color functions + gamma correction
Sam <sam@basx.dev>
parents: 74
diff changeset
7 ballcolor = RGBfromHex("B17F08").gamma(2.2)
74
779607656b12 fix: stuff, add working pong
Sam <sam@basx.dev>
parents:
diff changeset
8 levelRatio = 1
779607656b12 fix: stuff, add working pong
Sam <sam@basx.dev>
parents:
diff changeset
9 ballSize = 0.01'f
77
4b16d2af316a add: color functions + gamma correction
Sam <sam@basx.dev>
parents: 74
diff changeset
10 backgroundColor = RGBAfromHex("FAC034").gamma(2.2)
4b16d2af316a add: color functions + gamma correction
Sam <sam@basx.dev>
parents: 74
diff changeset
11 ballSpeed = 60'f
74
779607656b12 fix: stuff, add working pong
Sam <sam@basx.dev>
parents:
diff changeset
12
779607656b12 fix: stuff, add working pong
Sam <sam@basx.dev>
parents:
diff changeset
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
779607656b12 fix: stuff, add working pong
Sam <sam@basx.dev>
parents:
diff changeset
16
145
a4e6e76128e6 add: upgrade all simple examples to new engine version
Sam <sam@basx.dev>
parents: 85
diff changeset
17 #[
74
779607656b12 fix: stuff, add working pong
Sam <sam@basx.dev>
parents:
diff changeset
18 proc globalUpdate(engine: var Engine; t, dt: float32) =
779607656b12 fix: stuff, add working pong
Sam <sam@basx.dev>
parents:
diff changeset
19 var height = float32(engine.vulkan.frameSize.y) / float32(
779607656b12 fix: stuff, add working pong
Sam <sam@basx.dev>
parents:
diff changeset
20 engine.vulkan.frameSize.x)
779607656b12 fix: stuff, add working pong
Sam <sam@basx.dev>
parents:
diff changeset
21 var width = 1'f
779607656b12 fix: stuff, add working pong
Sam <sam@basx.dev>
parents:
diff changeset
22 uniforms.view.value = ortho[float32](
779607656b12 fix: stuff, add working pong
Sam <sam@basx.dev>
parents:
diff changeset
23 0'f, width,
779607656b12 fix: stuff, add working pong
Sam <sam@basx.dev>
parents:
diff changeset
24 0'f, height,
779607656b12 fix: stuff, add working pong
Sam <sam@basx.dev>
parents:
diff changeset
25 0'f, 1'f,
779607656b12 fix: stuff, add working pong
Sam <sam@basx.dev>
parents:
diff changeset
26 )
779607656b12 fix: stuff, add working pong
Sam <sam@basx.dev>
parents:
diff changeset
27 engine.vulkan.device.updateUniformData(pipeline, uniforms)
779607656b12 fix: stuff, add working pong
Sam <sam@basx.dev>
parents:
diff changeset
28 var player = level.firstWithName("player")
779607656b12 fix: stuff, add working pong
Sam <sam@basx.dev>
parents:
diff changeset
29 if Down in engine.input.keysDown and (player.transform.col(3).y + barSize/2) < height:
779607656b12 fix: stuff, add working pong
Sam <sam@basx.dev>
parents:
diff changeset
30 player.transform = player.transform * translate3d(0'f, 1'f * dt, 0'f)
779607656b12 fix: stuff, add working pong
Sam <sam@basx.dev>
parents:
diff changeset
31 if Up in engine.input.keysDown and (player.transform.col(3).y - barSize/2) > 0:
779607656b12 fix: stuff, add working pong
Sam <sam@basx.dev>
parents:
diff changeset
32 player.transform = player.transform * translate3d(0'f, -1'f * dt, 0'f)
779607656b12 fix: stuff, add working pong
Sam <sam@basx.dev>
parents:
diff changeset
33
779607656b12 fix: stuff, add working pong
Sam <sam@basx.dev>
parents:
diff changeset
34 var ball = level.firstWithName("ball")
779607656b12 fix: stuff, add working pong
Sam <sam@basx.dev>
parents:
diff changeset
35 ball.transform = ball.transform * translate3d(ballVelocity[0] * dt,
779607656b12 fix: stuff, add working pong
Sam <sam@basx.dev>
parents:
diff changeset
36 ballVelocity[1] * dt, 0'f)
779607656b12 fix: stuff, add working pong
Sam <sam@basx.dev>
parents:
diff changeset
37
779607656b12 fix: stuff, add working pong
Sam <sam@basx.dev>
parents:
diff changeset
38 # loose
779607656b12 fix: stuff, add working pong
Sam <sam@basx.dev>
parents:
diff changeset
39 if ball.transform.col(3).x - ballSize/2 <= 0:
77
4b16d2af316a add: color functions + gamma correction
Sam <sam@basx.dev>
parents: 74
diff changeset
40 ballVelocity = Vec2([1'f, 1'f]).normalized * ballSpeed
4b16d2af316a add: color functions + gamma correction
Sam <sam@basx.dev>
parents: 74
diff changeset
41 ball.transform[0, 3] = width / 2
4b16d2af316a add: color functions + gamma correction
Sam <sam@basx.dev>
parents: 74
diff changeset
42 ball.transform[1, 3] = height / 2
74
779607656b12 fix: stuff, add working pong
Sam <sam@basx.dev>
parents:
diff changeset
43
779607656b12 fix: stuff, add working pong
Sam <sam@basx.dev>
parents:
diff changeset
44 # bounce level
779607656b12 fix: stuff, add working pong
Sam <sam@basx.dev>
parents:
diff changeset
45 if ball.transform.col(3).x + ballSize/2 > width: ballVelocity[
779607656b12 fix: stuff, add working pong
Sam <sam@basx.dev>
parents:
diff changeset
46 0] = -ballVelocity[0]
779607656b12 fix: stuff, add working pong
Sam <sam@basx.dev>
parents:
diff changeset
47 if ball.transform.col(3).y - ballSize/2 <= 0: ballVelocity[1] = -ballVelocity[1]
779607656b12 fix: stuff, add working pong
Sam <sam@basx.dev>
parents:
diff changeset
48 if ball.transform.col(3).y + ballSize/2 > height: ballVelocity[
779607656b12 fix: stuff, add working pong
Sam <sam@basx.dev>
parents:
diff changeset
49 1] = -ballVelocity[1]
779607656b12 fix: stuff, add working pong
Sam <sam@basx.dev>
parents:
diff changeset
50
779607656b12 fix: stuff, add working pong
Sam <sam@basx.dev>
parents:
diff changeset
51 # bar
779607656b12 fix: stuff, add working pong
Sam <sam@basx.dev>
parents:
diff changeset
52 if ball.transform.col(3).x - ballSize/2 <= barWidth:
779607656b12 fix: stuff, add working pong
Sam <sam@basx.dev>
parents:
diff changeset
53 let
779607656b12 fix: stuff, add working pong
Sam <sam@basx.dev>
parents:
diff changeset
54 barTop = player.transform.col(3).y - barSize/2
779607656b12 fix: stuff, add working pong
Sam <sam@basx.dev>
parents:
diff changeset
55 barBottom = player.transform.col(3).y + barSize/2
779607656b12 fix: stuff, add working pong
Sam <sam@basx.dev>
parents:
diff changeset
56 ballTop = ball.transform.col(3).y - ballSize/2
779607656b12 fix: stuff, add working pong
Sam <sam@basx.dev>
parents:
diff changeset
57 ballBottom = ball.transform.col(3).y + ballSize/2
779607656b12 fix: stuff, add working pong
Sam <sam@basx.dev>
parents:
diff changeset
58 if ballTop >= barTop and ballBottom <= barBottom:
779607656b12 fix: stuff, add working pong
Sam <sam@basx.dev>
parents:
diff changeset
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
779607656b12 fix: stuff, add working pong
Sam <sam@basx.dev>
parents:
diff changeset
61
779607656b12 fix: stuff, add working pong
Sam <sam@basx.dev>
parents:
diff changeset
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
779607656b12 fix: stuff, add working pong
Sam <sam@basx.dev>
parents:
diff changeset
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
779607656b12 fix: stuff, add working pong
Sam <sam@basx.dev>
parents:
diff changeset
70 player.transform = translate3d(0'f, 0.3'f, 0'f)
779607656b12 fix: stuff, add working pong
Sam <sam@basx.dev>
parents:
diff changeset
71 level.add player
779607656b12 fix: stuff, add working pong
Sam <sam@basx.dev>
parents:
diff changeset
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
779607656b12 fix: stuff, add working pong
Sam <sam@basx.dev>
parents:
diff changeset
75 for i in 0 ..< ballmesh.vertexData.color.data.len:
779607656b12 fix: stuff, add working pong
Sam <sam@basx.dev>
parents:
diff changeset
76 ballmesh.vertexData.color.data[i] = ballcolor
779607656b12 fix: stuff, add working pong
Sam <sam@basx.dev>
parents:
diff changeset
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
779607656b12 fix: stuff, add working pong
Sam <sam@basx.dev>
parents:
diff changeset
79 ball.transform = scale3d(ballSize, ballSize, 1'f) * translate3d(10'f, 10'f, 0'f)
779607656b12 fix: stuff, add working pong
Sam <sam@basx.dev>
parents:
diff changeset
80 level.add ball
779607656b12 fix: stuff, add working pong
Sam <sam@basx.dev>
parents:
diff changeset
81
77
4b16d2af316a add: color functions + gamma correction
Sam <sam@basx.dev>
parents: 74
diff changeset
82 pipeline.clearColor = backgroundColor
74
779607656b12 fix: stuff, add working pong
Sam <sam@basx.dev>
parents:
diff changeset
83 # show something
779607656b12 fix: stuff, add working pong
Sam <sam@basx.dev>
parents:
diff changeset
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()