Spielemaschine als UNterbau für Spiele mit der ZM

This commit is contained in:
ngb
2022-06-29 22:29:42 +02:00
parent 57afc4683f
commit 67610963f2

View File

@@ -0,0 +1,102 @@
package schule.ngb.zm;
import java.awt.Graphics;
import java.util.LinkedList;
public class Spielemaschine extends Zeichenmaschine {
private LinkedList<Drawable> drawables;
private LinkedList<Updatable> updatables;
public Spielemaschine( String title ) {
this(STD_WIDTH, STD_HEIGHT, title);
}
private GraphicsLayer mainLayer;
public Spielemaschine( int width, int height, String title ) {
super(width, height, title, false);
drawables = new LinkedList<>();
updatables = new LinkedList<>();
canvas.removeLayers(drawing, shapes);
drawing = null;
shapes = null;
mainLayer = new GraphicsLayer();
canvas.addLayer(mainLayer);
}
public final void add( Object... pObjects ) {
for( Object obj : pObjects ) {
if( obj instanceof Drawable ) {
addDrawable((Drawable) obj);
}
if( obj instanceof Updatable ) {
addUpdatable((Updatable) obj);
}
}
}
public final void addDrawable( Drawable pDrawable ) {
synchronized( drawables ) {
drawables.add(pDrawable);
}
}
public final void addUpdatable( Updatable pUpdatable ) {
synchronized( updatables ) {
updatables.add(pUpdatable);
}
}
public final void remove( Object... pObjects ) {
for( Object obj : pObjects ) {
if( obj instanceof Drawable ) {
removeDrawable((Drawable) obj);
}
if( obj instanceof Updatable ) {
removeUpdatable((Updatable) obj);
}
}
}
public final void removeDrawable( Drawable pDrawable ) {
synchronized( drawables ) {
drawables.remove(pDrawable);
}
}
public final void removeUpdatable( Updatable pUpdatable ) {
synchronized( updatables ) {
updatables.remove(pUpdatable);
}
}
@Override
public final void update( double delta ) {
synchronized( updatables ) {
for( Updatable updatable : updatables ) {
if( updatable.isActive() ) {
updatable.update(delta);
}
}
}
}
@Override
public final void draw() {
mainLayer.clear();
synchronized( drawables ) {
for( Drawable drawable : drawables ) {
if( drawable.isVisible() ) {
drawable.draw(mainLayer.getGraphics());
}
}
}
}
}