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