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