1 module app;
2 
3 import unecht;
4 
5 import ball;
6 import paddle;
7 import field;
8 
9 import derelict.imgui.imgui;
10 
11 ///
12 @UEDefaultInspector!TestControls
13 final class TestControls : UEComponent
14 {
15     mixin(UERegisterComponent!());
16 
17     static UEEntity balls;
18     static int ballCount;
19 
20     Field field;
21 
22     float lastBallCreated;
23 
24     int scoreLeft;
25     int scoreRight;
26 
27     override void onCreate() {
28         super.onCreate;
29 
30         field = entity.addComponent!Field;
31 
32         registerEvent(UEEventType.key, &OnKeyEvent);
33 
34         balls = UEEntity.create("balls");
35 
36         spawnBall();
37         spawnPaddle(false);
38         spawnPaddle(true);
39     }
40 
41     override void onUpdate() {
42         super.onUpdate;
43 
44         if(ue.tickTime - lastBallCreated > ballCount*2)
45             spawnBall();
46 
47         ig_PushStyleColor(ImGuiCol_WindowBg, ImVec4(1,1,1,0));
48         auto labelWidth = 100;
49         ig_SetNextWindowPos(ImVec2((ue.application.mainWindow.size.width-labelWidth)/2));
50         ig_SetNextWindowSize(ImVec2(labelWidth,-1),ImGuiSetCond_Once);
51         ig_Begin("",null,ImGuiWindowFlags_NoMove|ImGuiWindowFlags_NoTitleBar|ImGuiWindowFlags_NoResize);
52         UEGui.Text(.format("%s - %s",scoreLeft,scoreRight));
53         ig_End();
54         ig_PopStyleColor();
55     }
56 
57     void OnKeyEvent(UEEvent _ev)
58     {
59         if(_ev.keyEvent.action == UEEvent.KeyEvent.Action.Down)
60         {
61             if(_ev.keyEvent.key == UEKey.esc)
62                 ue.application.terminate();
63         }
64     }
65 
66     void spawnBall()
67     {
68         auto newE = UEEntity.create("ball",balls.sceneNode);
69         import std.random:uniform;
70         newE.sceneNode.position = vec3(uniform(0.0f,1),1,uniform(0.0f,1));
71         auto ballLogic = newE.addComponent!BallLogic;
72         ballLogic.controls = this;
73 
74         lastBallCreated = ue.tickTime;
75         ballCount++;
76     }
77 
78     void onBallOut(UEEntity border)
79     {
80         if(field.isLeft(border))
81             scoreRight++;
82         else
83             scoreLeft++;
84     }
85 
86     static void spawnPaddle(bool rightSide)
87     {
88         float side = rightSide?1:-1;
89 
90         auto newE = UEEntity.create("paddle");
91         import std.random:uniform;
92         newE.sceneNode.position = vec3(-14.5*side,1,0);
93         newE.sceneNode.scaling = vec3(0.5,1,2);
94 
95         auto paddleLogic = newE.addComponent!PaddleLogic;
96 
97         if(!rightSide)
98         {
99             paddleLogic.keyUp = UEKey.r;
100             paddleLogic.keyDown = UEKey.f;
101         }
102     }
103 }
104 
105 shared static this()
106 {
107 	ue.windowSettings.size.width = 1024;
108 	ue.windowSettings.size.height = 768;
109 	ue.windowSettings.title = "unecht - pong sample";
110 
111 	ue.hookStartup = () {
112         UEPhysicsSystem.setGravity(vec3(0));
113 
114 		auto newE = UEEntity.create("game");
115         newE.addComponent!TestControls;
116 
117         import unecht.core.components.camera;
118 		auto newE2 = UEEntity.create("camera entity");
119 		newE2.sceneNode.position = vec3(0,30,0);
120         newE2.sceneNode.angles = vec3(90,0,0);
121 		auto cam = newE2.addComponent!UECamera;
122         cam.isOrthographic = true;
123         cam.orthoSize = 30.0f;
124 	};
125 }