@@ -41,6 +41,7 @@ public Scene() {
4141 public void instantiate (Component component ) {
4242 component .start ();
4343 newComponents .add (component );
44+ validate ();
4445 }
4546
4647 public void startScene () {
@@ -139,6 +140,14 @@ public void setCamera(Camera camera) {
139140 * Updates all the components and calculates delta time and fps
140141 */
141142 public void update () {
143+ if (newComponents .size () > 0 ) {
144+ components .addAll (newComponents );
145+ newComponents .clear ();
146+ }
147+ if (remove .size () > 0 ) {
148+ components .removeAll (remove );
149+ remove .clear ();
150+ }
142151 time += JavaGameEngine .deltaTime ;
143152 if ((int ) time / 100 > lastSec ) {
144153 lastSec = (int ) (time / 100 );
@@ -161,14 +170,6 @@ public void update() {
161170
162171 camera .update ();
163172
164- if (newComponents .size () > 0 ) {
165- components .addAll (newComponents );
166- newComponents .clear ();
167- }
168- if (remove .size () > 0 ) {
169- components .removeAll (remove );
170- remove .clear ();
171- }
172173 Input .setMousePressed (1000 );
173174 }
174175
@@ -184,12 +185,14 @@ public void destroy(Component c) {
184185 public boolean inside (Component component ) {
185186 return screen .contains (component .getShape ().getBounds ());
186187 }
187-
188+ public static void playSound (String path ){
189+ playSound (path ,JavaGameEngine .masterVolume );
190+ }
188191 /**
189192 * Plays a sound from path
190193 * @param path path
191194 */
192- public static void playSound (String path ) {
195+ public static void playSound (String path , float volume ) {
193196 new Thread (new Runnable () {
194197 @ Override
195198 public void run () {
@@ -217,9 +220,14 @@ public synchronized void waitUntilDone() throws InterruptedException {
217220 AudioInputStream audioInputStream = AudioSystem .getAudioInputStream (JavaGameEngine .class .getResourceAsStream (path ));
218221 try {
219222 Clip clip = AudioSystem .getClip ();
223+ if (volume < 0f || volume > 1f ) throw new IllegalArgumentException ("Volume not valid: " + volume );
224+
225+
220226 clip .addLineListener (listener );
221227 clip .open (audioInputStream );
222228 try {
229+ FloatControl gainControl = (FloatControl ) clip .getControl (FloatControl .Type .MASTER_GAIN );
230+ gainControl .setValue (20f * (float ) Math .log10 (volume ));
223231 clip .start ();
224232 listener .waitUntilDone ();
225233 } finally {
@@ -270,28 +278,27 @@ public int compare(Component o1, Component o2){
270278 }
271279 });
272280
273-
274281 try {
275282 int lsize = components .size ();
276283 for (int i = 0 ; i < lsize ;i ++){
277284 Component c = renderList .get (i );
278285 if (inside (c )) {
279286 (c ).render (graphics2D );
280- if (!c .isVisible ()){
281- c .setVisible (true );
282- c .onCameraEnter ();
283- }
284- }
285- else if (c .isVisible ()){
286- c .setVisible (false );
287- c .onCameraLeft ();
287+ //if(!c.isVisible()){
288+ // c.setVisible(true);
289+ // c.onCameraEnter();
290+ //}
288291 }
292+ //else if(c.isVisible()){
293+ // c.setVisible(false);
294+ // c.onCameraLeft();
295+ //}
289296 }
290- //graphics2D.translate(width*percentW,height*percentH);
291- graphics2D .translate (-camera .getPosition ().getX (),-camera .getPosition ().getY ());
292- //graphics2D.scale(1/scale.getX(),1/scale.getY());
297+ //graphics2D.translate(width*percentW,height*percentH);
298+ //graphics2D.scale(1/scale.getX(),1/scale.getY());
293299
294- LightManager .render (graphics2D );
300+ graphics2D .translate (-camera .getPosition ().getX (),-camera .getPosition ().getY ());
301+ LightManager .render (graphics2D );
295302
296303 }catch (Exception e ){}
297304 }
0 commit comments