mirror of
https://github.com/jneug/zeichenmaschine.git
synced 2026-04-14 14:43:33 +02:00
Spielemaschine als UNterbau für Spiele mit der ZM
This commit is contained in:
102
src/schule/ngb/zm/Spielemaschine.java
Normal file
102
src/schule/ngb/zm/Spielemaschine.java
Normal 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());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user