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