annotate examples/E10_pong.nim @ 74:779607656b12

fix: stuff, add working pong
author Sam <sam@basx.dev>
date Mon, 06 Feb 2023 23:32:59 +0700
parents
children 4b16d2af316a
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 type
779607656b12 fix: stuff, add working pong
Sam <sam@basx.dev>
parents:
diff changeset
4 Vertex = object
779607656b12 fix: stuff, add working pong
Sam <sam@basx.dev>
parents:
diff changeset
5 position: PositionAttribute[Vec2]
779607656b12 fix: stuff, add working pong
Sam <sam@basx.dev>
parents:
diff changeset
6 color: ColorAttribute[Vec3]
779607656b12 fix: stuff, add working pong
Sam <sam@basx.dev>
parents:
diff changeset
7 transform: ModelTransformAttribute
779607656b12 fix: stuff, add working pong
Sam <sam@basx.dev>
parents:
diff changeset
8 Uniforms = object
779607656b12 fix: stuff, add working pong
Sam <sam@basx.dev>
parents:
diff changeset
9 view: ViewProjectionTransform
779607656b12 fix: stuff, add working pong
Sam <sam@basx.dev>
parents:
diff changeset
10
779607656b12 fix: stuff, add working pong
Sam <sam@basx.dev>
parents:
diff changeset
11 const
779607656b12 fix: stuff, add working pong
Sam <sam@basx.dev>
parents:
diff changeset
12 barcolor = Vec3([1'f, 0'f, 0'f])
779607656b12 fix: stuff, add working pong
Sam <sam@basx.dev>
parents:
diff changeset
13 barSize = 0.1'f
779607656b12 fix: stuff, add working pong
Sam <sam@basx.dev>
parents:
diff changeset
14 barWidth = 0.01'f
779607656b12 fix: stuff, add working pong
Sam <sam@basx.dev>
parents:
diff changeset
15 ballcolor = Vec3([0'f, 0'f, 0'f])
779607656b12 fix: stuff, add working pong
Sam <sam@basx.dev>
parents:
diff changeset
16 levelRatio = 1
779607656b12 fix: stuff, add working pong
Sam <sam@basx.dev>
parents:
diff changeset
17 ballSize = 0.01'f
779607656b12 fix: stuff, add working pong
Sam <sam@basx.dev>
parents:
diff changeset
18
779607656b12 fix: stuff, add working pong
Sam <sam@basx.dev>
parents:
diff changeset
19 var
779607656b12 fix: stuff, add working pong
Sam <sam@basx.dev>
parents:
diff changeset
20 uniforms = Uniforms()
779607656b12 fix: stuff, add working pong
Sam <sam@basx.dev>
parents:
diff changeset
21 pipeline: RenderPipeline[Vertex, Uniforms]
779607656b12 fix: stuff, add working pong
Sam <sam@basx.dev>
parents:
diff changeset
22 level: Thing
779607656b12 fix: stuff, add working pong
Sam <sam@basx.dev>
parents:
diff changeset
23 ballVelocity = Vec2([1'f, 1'f]).normalized * 30'f
779607656b12 fix: stuff, add working pong
Sam <sam@basx.dev>
parents:
diff changeset
24
779607656b12 fix: stuff, add working pong
Sam <sam@basx.dev>
parents:
diff changeset
25 proc globalUpdate(engine: var Engine; t, dt: float32) =
779607656b12 fix: stuff, add working pong
Sam <sam@basx.dev>
parents:
diff changeset
26 var height = float32(engine.vulkan.frameSize.y) / float32(
779607656b12 fix: stuff, add working pong
Sam <sam@basx.dev>
parents:
diff changeset
27 engine.vulkan.frameSize.x)
779607656b12 fix: stuff, add working pong
Sam <sam@basx.dev>
parents:
diff changeset
28 var width = 1'f
779607656b12 fix: stuff, add working pong
Sam <sam@basx.dev>
parents:
diff changeset
29 uniforms.view.value = ortho[float32](
779607656b12 fix: stuff, add working pong
Sam <sam@basx.dev>
parents:
diff changeset
30 0'f, width,
779607656b12 fix: stuff, add working pong
Sam <sam@basx.dev>
parents:
diff changeset
31 0'f, height,
779607656b12 fix: stuff, add working pong
Sam <sam@basx.dev>
parents:
diff changeset
32 0'f, 1'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 engine.vulkan.device.updateUniformData(pipeline, uniforms)
779607656b12 fix: stuff, add working pong
Sam <sam@basx.dev>
parents:
diff changeset
35 var player = level.firstWithName("player")
779607656b12 fix: stuff, add working pong
Sam <sam@basx.dev>
parents:
diff changeset
36 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
37 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
38 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
39 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
40
779607656b12 fix: stuff, add working pong
Sam <sam@basx.dev>
parents:
diff changeset
41 var ball = level.firstWithName("ball")
779607656b12 fix: stuff, add working pong
Sam <sam@basx.dev>
parents:
diff changeset
42 ball.transform = ball.transform * translate3d(ballVelocity[0] * dt,
779607656b12 fix: stuff, add working pong
Sam <sam@basx.dev>
parents:
diff changeset
43 ballVelocity[1] * dt, 0'f)
779607656b12 fix: stuff, add working pong
Sam <sam@basx.dev>
parents:
diff changeset
44
779607656b12 fix: stuff, add working pong
Sam <sam@basx.dev>
parents:
diff changeset
45 # loose
779607656b12 fix: stuff, add working pong
Sam <sam@basx.dev>
parents:
diff changeset
46 if ball.transform.col(3).x - ballSize/2 <= 0:
779607656b12 fix: stuff, add working pong
Sam <sam@basx.dev>
parents:
diff changeset
47 ballVelocity = Vec2([1'f, 1'f]).normalized * 30'f
779607656b12 fix: stuff, add working pong
Sam <sam@basx.dev>
parents:
diff changeset
48 ball.transform[0, 3] = 0.5
779607656b12 fix: stuff, add working pong
Sam <sam@basx.dev>
parents:
diff changeset
49 ball.transform[1, 3] = 0.5
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 # bounce level
779607656b12 fix: stuff, add working pong
Sam <sam@basx.dev>
parents:
diff changeset
52 if ball.transform.col(3).x + ballSize/2 > width: ballVelocity[
779607656b12 fix: stuff, add working pong
Sam <sam@basx.dev>
parents:
diff changeset
53 0] = -ballVelocity[0]
779607656b12 fix: stuff, add working pong
Sam <sam@basx.dev>
parents:
diff changeset
54 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
55 if ball.transform.col(3).y + ballSize/2 > height: ballVelocity[
779607656b12 fix: stuff, add working pong
Sam <sam@basx.dev>
parents:
diff changeset
56 1] = -ballVelocity[1]
779607656b12 fix: stuff, add working pong
Sam <sam@basx.dev>
parents:
diff changeset
57
779607656b12 fix: stuff, add working pong
Sam <sam@basx.dev>
parents:
diff changeset
58 # bar
779607656b12 fix: stuff, add working pong
Sam <sam@basx.dev>
parents:
diff changeset
59 if ball.transform.col(3).x - ballSize/2 <= barWidth:
779607656b12 fix: stuff, add working pong
Sam <sam@basx.dev>
parents:
diff changeset
60 let
779607656b12 fix: stuff, add working pong
Sam <sam@basx.dev>
parents:
diff changeset
61 barTop = player.transform.col(3).y - barSize/2
779607656b12 fix: stuff, add working pong
Sam <sam@basx.dev>
parents:
diff changeset
62 barBottom = player.transform.col(3).y + barSize/2
779607656b12 fix: stuff, add working pong
Sam <sam@basx.dev>
parents:
diff changeset
63 ballTop = ball.transform.col(3).y - ballSize/2
779607656b12 fix: stuff, add working pong
Sam <sam@basx.dev>
parents:
diff changeset
64 ballBottom = ball.transform.col(3).y + ballSize/2
779607656b12 fix: stuff, add working pong
Sam <sam@basx.dev>
parents:
diff changeset
65 if ballTop >= barTop and ballBottom <= barBottom:
779607656b12 fix: stuff, add working pong
Sam <sam@basx.dev>
parents:
diff changeset
66 ballVelocity[0] = abs(ballVelocity[0])
779607656b12 fix: stuff, add working pong
Sam <sam@basx.dev>
parents:
diff changeset
67
779607656b12 fix: stuff, add working pong
Sam <sam@basx.dev>
parents:
diff changeset
68
779607656b12 fix: stuff, add working pong
Sam <sam@basx.dev>
parents:
diff changeset
69 when isMainModule:
779607656b12 fix: stuff, add working pong
Sam <sam@basx.dev>
parents:
diff changeset
70 var myengine = igniteEngine("Pong")
779607656b12 fix: stuff, add working pong
Sam <sam@basx.dev>
parents:
diff changeset
71 myengine.fps = 60
779607656b12 fix: stuff, add working pong
Sam <sam@basx.dev>
parents:
diff changeset
72 level = newThing("Level")
779607656b12 fix: stuff, add working pong
Sam <sam@basx.dev>
parents:
diff changeset
73 var playerbarmesh = quad[Vertex, Vec2, float32]()
779607656b12 fix: stuff, add working pong
Sam <sam@basx.dev>
parents:
diff changeset
74 playerbarmesh.vertexData.color.data = @[barcolor, barcolor, barcolor, barcolor]
779607656b12 fix: stuff, add working pong
Sam <sam@basx.dev>
parents:
diff changeset
75 playerbarmesh.vertexData.transform.data = @[Unit44]
779607656b12 fix: stuff, add working pong
Sam <sam@basx.dev>
parents:
diff changeset
76 var playerbar = newThing("playerbar", playerbarmesh)
779607656b12 fix: stuff, add working pong
Sam <sam@basx.dev>
parents:
diff changeset
77 playerbar.transform = scale3d(barWidth, barSize, 1'f) * translate3d(0.5'f,
779607656b12 fix: stuff, add working pong
Sam <sam@basx.dev>
parents:
diff changeset
78 0'f, 0'f)
779607656b12 fix: stuff, add working pong
Sam <sam@basx.dev>
parents:
diff changeset
79 var player = newThing("player", playerbar)
779607656b12 fix: stuff, add working pong
Sam <sam@basx.dev>
parents:
diff changeset
80 player.transform = translate3d(0'f, 0.3'f, 0'f)
779607656b12 fix: stuff, add working pong
Sam <sam@basx.dev>
parents:
diff changeset
81 level.add player
779607656b12 fix: stuff, add working pong
Sam <sam@basx.dev>
parents:
diff changeset
82
779607656b12 fix: stuff, add working pong
Sam <sam@basx.dev>
parents:
diff changeset
83 var ballmesh = circle[Vertex, Vec2, float32]()
779607656b12 fix: stuff, add working pong
Sam <sam@basx.dev>
parents:
diff changeset
84 ballmesh.vertexData.color.data = newSeq[Vec3](
779607656b12 fix: stuff, add working pong
Sam <sam@basx.dev>
parents:
diff changeset
85 ballmesh.vertexData.position.data.len)
779607656b12 fix: stuff, add working pong
Sam <sam@basx.dev>
parents:
diff changeset
86 for i in 0 ..< ballmesh.vertexData.color.data.len:
779607656b12 fix: stuff, add working pong
Sam <sam@basx.dev>
parents:
diff changeset
87 ballmesh.vertexData.color.data[i] = ballcolor
779607656b12 fix: stuff, add working pong
Sam <sam@basx.dev>
parents:
diff changeset
88 ballmesh.vertexData.transform.data = @[Unit44]
779607656b12 fix: stuff, add working pong
Sam <sam@basx.dev>
parents:
diff changeset
89 var ball = newThing("ball", ballmesh)
779607656b12 fix: stuff, add working pong
Sam <sam@basx.dev>
parents:
diff changeset
90 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
91 level.add ball
779607656b12 fix: stuff, add working pong
Sam <sam@basx.dev>
parents:
diff changeset
92
779607656b12 fix: stuff, add working pong
Sam <sam@basx.dev>
parents:
diff changeset
93 # upload data, prepare shaders, etc
779607656b12 fix: stuff, add working pong
Sam <sam@basx.dev>
parents:
diff changeset
94 const vertexShader = generateVertexShaderCode[Vertex, Uniforms]()
779607656b12 fix: stuff, add working pong
Sam <sam@basx.dev>
parents:
diff changeset
95 const fragmentShader = generateFragmentShaderCode[Vertex]()
779607656b12 fix: stuff, add working pong
Sam <sam@basx.dev>
parents:
diff changeset
96 pipeline = setupPipeline[Vertex, Uniforms, uint16](
779607656b12 fix: stuff, add working pong
Sam <sam@basx.dev>
parents:
diff changeset
97 myengine,
779607656b12 fix: stuff, add working pong
Sam <sam@basx.dev>
parents:
diff changeset
98 level,
779607656b12 fix: stuff, add working pong
Sam <sam@basx.dev>
parents:
diff changeset
99 vertexShader,
779607656b12 fix: stuff, add working pong
Sam <sam@basx.dev>
parents:
diff changeset
100 fragmentShader
779607656b12 fix: stuff, add working pong
Sam <sam@basx.dev>
parents:
diff changeset
101 )
779607656b12 fix: stuff, add working pong
Sam <sam@basx.dev>
parents:
diff changeset
102 pipeline.clearColor = Vec4([1.0'f, 1.0'f, 1.0'f, 1.0'f])
779607656b12 fix: stuff, add working pong
Sam <sam@basx.dev>
parents:
diff changeset
103 # show something
779607656b12 fix: stuff, add working pong
Sam <sam@basx.dev>
parents:
diff changeset
104 myengine.run(pipeline, globalUpdate)
779607656b12 fix: stuff, add working pong
Sam <sam@basx.dev>
parents:
diff changeset
105 pipeline.trash()
779607656b12 fix: stuff, add working pong
Sam <sam@basx.dev>
parents:
diff changeset
106 myengine.trash()