package org.gbcpainter.game;
import org.gbcpainter.game.model.Monster;
import org.gbcpainter.game.model.Player;
import org.gbcpainter.game.model.PlayerImpl;
import org.gbcpainter.game.model.grid.Junction;
import org.gbcpainter.game.model.grid.JunctionImpl;
import org.gbcpainter.game.model.grid.Pipe;
import org.gbcpainter.game.model.grid.PipeImpl;
import org.gbcpainter.game.model.monsters.Bomber;
import org.gbcpainter.game.model.monsters.Gremlin;
import org.gbcpainter.game.model.monsters.Sponge;
import org.gbcpainter.geom.PERPENDICULAR_DIRECTION;
import org.gbcpainter.geom.Segment;
import org.gbcpainter.loaders.level.IGameElementFactory;
import org.jetbrains.annotations.NonNls;
import org.jetbrains.annotations.NotNull;
import java.awt.*;
import java.util.EnumSet;
import java.util.Set;
/**
* Implementation of the {@link org.gbcpainter.loaders.level.IGameElementFactory}
* <p/>
* Monsters must have the names "Gremlin", "Sponge" and "Bomber"
*
* @author Lorenzo Pellegrini
*/
public class GameElementFactory implements IGameElementFactory<Player, Monster, Junction, Pipe> {
@NonNls
private static final String GREMLIN = "Gremlin";
@NonNls
private static final String SPONGE = "Sponge";
@NonNls
private static final String BOMBER = "Bomber";
@NotNull
@Override
public Player createPlayer( @NotNull final Point position, @NotNull final PERPENDICULAR_DIRECTION direction ) throws IllegalArgumentException {
final Player result = createPlayer( position );
result.setDirection( direction );
return result;
}
@NotNull
@Override
public Player createPlayer( @NotNull final Point position ) throws IllegalArgumentException {
try {
return new PlayerImpl( position );
} catch ( Exception e ) {
throw new IllegalArgumentException( "Error while defining the plater" );
}
}
@NotNull
@Override
public Monster createMonster( @NonNls final String name, final Point position, final PERPENDICULAR_DIRECTION direction ) throws IllegalArgumentException {
final Monster mob = createMonster( name, position );
mob.setDirection( direction );
return mob;
}
@NotNull
@Override
public Monster createMonster( @NonNls final String name, final Point position ) throws IllegalArgumentException {
if ( name.equalsIgnoreCase( GREMLIN ) ) {
try {
return new Gremlin( position );
} catch ( Exception e ) {
throw new IllegalArgumentException( e );
}
} else if ( name.equalsIgnoreCase( SPONGE ) ) {
try {
return new Sponge( position );
} catch ( Exception e ) {
throw new IllegalArgumentException( e );
}
} else if ( name.equalsIgnoreCase( BOMBER ) ) {
try {
return new Bomber( position );
} catch ( Exception e ) {
throw new IllegalArgumentException( e );
}
} else {
throw new IllegalArgumentException( "Invalid monster name " + name );
}
}
@NotNull
@Override
public Junction createJunction( @NotNull final Point position, @NotNull final Set<Segment> segments ) throws IllegalArgumentException {
Set<PERPENDICULAR_DIRECTION> directions = EnumSet.noneOf( PERPENDICULAR_DIRECTION.class );
for (Segment exitPipe : segments) {
final Point otherPoint = position.equals( exitPipe.getA() ) ? exitPipe.getB() : exitPipe.getA();
if ( otherPoint.x == position.x ) {
if ( otherPoint.y < position.y ) {
directions.add( PERPENDICULAR_DIRECTION.UP );
} else {
directions.add( PERPENDICULAR_DIRECTION.DOWN );
}
} else {
if ( otherPoint.x < position.x ) {
directions.add( PERPENDICULAR_DIRECTION.LEFT );
} else {
directions.add( PERPENDICULAR_DIRECTION.RIGHT );
}
}
}
try {
return new JunctionImpl( position, directions );
} catch ( Exception e ) {
throw new IllegalArgumentException( e );
}
}
@NotNull
@Override
public Pipe createEdge( @NotNull final Segment segment ) throws IllegalArgumentException {
try {
return new PipeImpl( segment );
} catch ( Exception e ) {
throw new IllegalArgumentException( e );
}
}
}