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