mirror of
https://github.com/inf112-v20/Fiasko.git
synced 2025-01-31 15:19:35 +01:00
Oppdaterer lasere for å ta hensyn til alle mulige situasjoner. Closes #69
Det tas nå hensyn til enkle lasere som skyter mot doble lasere, enkle lasere som krysser doble lasere, lasere som krysser triple lasere, og egentlig alle situasjoner som kan tenkes.
This commit is contained in:
parent
fca361b750
commit
232d2d9a8a
@ -7,19 +7,39 @@ public enum ParticleType {
|
||||
/**
|
||||
* The beam emitting from a single laser
|
||||
*/
|
||||
LASER_BEAM_SINGLE(1),
|
||||
LASER_BEAM_SINGLE(10),
|
||||
/**
|
||||
* The beam emitting from a double laser
|
||||
*/
|
||||
LASER_BEAM_DOUBLE(2),
|
||||
LASER_BEAM_DOUBLE(20),
|
||||
/**
|
||||
* The beam emitting if a single laser shoots in the opposite direction of a double laser
|
||||
*/
|
||||
LASER_BEAM_TRIPLE(30),
|
||||
/**
|
||||
* The beam emitted where two single beams cross
|
||||
*/
|
||||
LASER_BEAM_SINGLE_CROSS(3),
|
||||
LASER_BEAM_SINGLE_CROSS(11),
|
||||
/**
|
||||
* The beam emitted where two double beams cross
|
||||
*/
|
||||
LASER_BEAM_DOUBLE_CROSS(4);
|
||||
LASER_BEAM_DOUBLE_CROSS(22),
|
||||
/**
|
||||
* The beam emitted where two triple beams cross
|
||||
*/
|
||||
LASER_BEAM_TRIPLE_CROSS(33),
|
||||
/**
|
||||
* The beam emitted where a single beam crosses a double beam
|
||||
*/
|
||||
LASER_BEAM_SINGLE_DOUBLE_CROSS(12),
|
||||
/**
|
||||
* The beam emitted where a single beam crosses a triple beam
|
||||
*/
|
||||
LASER_BEAM_SINGLE_TRIPLE_CROSS(13),
|
||||
/**
|
||||
* The beam emitted where a double beam crosses a triple beam
|
||||
*/
|
||||
LASER_BEAM_DOUBLE_TRIPLE_CROSS(23);
|
||||
|
||||
private final int particleTypeID;
|
||||
|
||||
|
@ -858,29 +858,111 @@ public class Board {
|
||||
* @param laserType The type of the laser shooting
|
||||
*/
|
||||
private void updateLaserBeamOnParticleGrid(Position addPosition, Direction laserDirection, WallType laserType) {
|
||||
ParticleType laserParticleType = laserType == WallType.WALL_LASER_SINGLE ? ParticleType.LASER_BEAM_SINGLE :
|
||||
ParticleType.LASER_BEAM_DOUBLE;
|
||||
Particle laserParticle = new Particle(laserParticleType, laserDirection);
|
||||
int positionX = addPosition.getXCoordinate();
|
||||
int positionY = addPosition.getYCoordinate();
|
||||
int numberOfLasers;
|
||||
switch (laserType) {
|
||||
case WALL_LASER_SINGLE:
|
||||
numberOfLasers = 1;
|
||||
break;
|
||||
case WALL_LASER_DOUBLE:
|
||||
numberOfLasers = 2;
|
||||
break;
|
||||
default:
|
||||
throw new IllegalArgumentException("Laser type submitted is not a laser.");
|
||||
}
|
||||
ParticleType type;
|
||||
Particle particleAtPosition = particles.getElement(positionX, positionY);
|
||||
if (particleAtPosition != null && Direction.arePerpendicular(particleAtPosition.getDirection(),
|
||||
laserDirection)) {
|
||||
type = numberOfLasers == 1 ? ParticleType.LASER_BEAM_SINGLE_CROSS :
|
||||
ParticleType.LASER_BEAM_DOUBLE_CROSS;
|
||||
if (particleAtPosition == null) {
|
||||
particles.setElement(positionX, positionY, laserParticle);
|
||||
} else {
|
||||
type = numberOfLasers == 1 ? ParticleType.LASER_BEAM_SINGLE : ParticleType.LASER_BEAM_DOUBLE;
|
||||
particles.setElement(positionX, positionY, getNewLaserBeamParticle(laserParticle, particleAtPosition));
|
||||
}
|
||||
particles.setElement(positionX, positionY, new Particle(type, laserDirection));
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the new particle to use given the laser firing and the existing beam particle
|
||||
*
|
||||
* @param laserBeam The laser beam which is fired
|
||||
* @param existingBeam The laser beam which already exists at a tile
|
||||
* @return The particle which is a combination of the two
|
||||
*/
|
||||
private Particle getNewLaserBeamParticle(Particle laserBeam, Particle existingBeam) {
|
||||
ParticleType laserBeamType = laserBeam.getParticleType();
|
||||
ParticleType existingBeamType = existingBeam.getParticleType();
|
||||
Direction laserDirection = laserBeam.getDirection();
|
||||
Direction existingDirection = existingBeam.getDirection();
|
||||
|
||||
int forwardBeamsLaser = getNumberOfForwardBeams(laserBeamType);
|
||||
int crossingBeamsLaser = getNumberOfPerpendicularBeams(laserBeamType);
|
||||
int forwardBeamsExisting = getNumberOfForwardBeams(existingBeamType);
|
||||
int crossingBeamsExisting = getNumberOfPerpendicularBeams(existingBeamType);
|
||||
|
||||
//Flip number of beams if beams are perpendicular
|
||||
if (Direction.arePerpendicular(laserDirection, existingDirection)) {
|
||||
int temp = forwardBeamsExisting;
|
||||
forwardBeamsExisting = crossingBeamsExisting;
|
||||
crossingBeamsExisting = temp;
|
||||
}
|
||||
//Calculates the new number of forward beams
|
||||
int forwardBeams = getNumberOfBeams(forwardBeamsLaser, forwardBeamsExisting);
|
||||
//Calculates the new number of crossing beams
|
||||
int crossingBeams = getNumberOfBeams(crossingBeamsLaser, crossingBeamsExisting);
|
||||
//The direction should be the direction with the least amount of beams
|
||||
Direction newDirection;
|
||||
if (forwardBeams > crossingBeams) {
|
||||
newDirection = existingDirection;
|
||||
} else {
|
||||
newDirection = laserDirection;
|
||||
}
|
||||
//If using the existing direction and the beams are perpendicular, the direction needs to be rotated
|
||||
if (newDirection == existingDirection &&
|
||||
Direction.arePerpendicular(laserDirection, existingDirection)) {
|
||||
newDirection = Direction.getLeftRotatedDirection(newDirection);
|
||||
}
|
||||
//If the particle does not exist, we have to rotate the beam to get the correct one
|
||||
ParticleType newParticleType = getNewParticleType(forwardBeams, crossingBeams);
|
||||
if (newParticleType == null) {
|
||||
newParticleType = getNewParticleType(crossingBeams, forwardBeams);
|
||||
newDirection = Direction.getLeftRotatedDirection(newDirection);
|
||||
}
|
||||
return new Particle(newParticleType, newDirection);
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the correct number of beams given existing beams and the beams to add
|
||||
*
|
||||
* @param newBeams The beam count of the new beam to add
|
||||
* @param existingBeams The beam count of the existing beam
|
||||
* @return The new number/thickness of beams/the beam
|
||||
*/
|
||||
private int getNumberOfBeams(int newBeams, int existingBeams) {
|
||||
if ((newBeams + existingBeams) != 0 &&(newBeams + existingBeams) % 3 == 0) {
|
||||
return 3;
|
||||
} else {
|
||||
return Math.max(newBeams, existingBeams);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets a new particle type with the given number of beams
|
||||
* @param forwardBeams The number of beams in the direction of the laser
|
||||
* @param perpendicularBeams The number of beams in the perpendicular direction of the laser
|
||||
* @return The correct particle type to be displayed
|
||||
*/
|
||||
private ParticleType getNewParticleType(int forwardBeams, int perpendicularBeams) {
|
||||
return ParticleType.getParticleTypeFromID(10 * forwardBeams + perpendicularBeams);
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the number of beams in the forward direction given a particle type
|
||||
*
|
||||
* @param particleType The type of particle to check
|
||||
* @return The number of beams in the forward direction of the laser beam
|
||||
*/
|
||||
private int getNumberOfForwardBeams(ParticleType particleType) {
|
||||
return particleType.getParticleTypeID() / 10;
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the number of beams in the perpendicular direction given a particle type
|
||||
*
|
||||
* @param particleType The type of particle to check
|
||||
* @return The number of beams in the perpendicular direction of the laser beam
|
||||
*/
|
||||
private int getNumberOfPerpendicularBeams(ParticleType particleType) {
|
||||
return particleType.getParticleTypeID() % 10;
|
||||
}
|
||||
|
||||
/**
|
||||
|
Binary file not shown.
Before Width: | Height: | Size: 17 MiB After Width: | Height: | Size: 17 MiB |
@ -1,4 +1,9 @@
|
||||
LASER_BEAM_SINGLE 6 5 6 4 6 5 6 4
|
||||
LASER_BEAM_DOUBLE 5 12 6 12 5 12 6 12
|
||||
LASER_BEAM_TRIPLE 7 7 7 8 7 7 7 8
|
||||
LASER_BEAM_SINGLE_CROSS 7 4
|
||||
LASER_BEAM_DOUBLE_CROSS 4 12
|
||||
LASER_BEAM_DOUBLE_CROSS 4 12
|
||||
LASER_BEAM_TRIPLE_CROSS 7 11
|
||||
LASER_BEAM_SINGLE_DOUBLE_CROSS 7 6 7 5 7 6 7 5
|
||||
LASER_BEAM_SINGLE_TRIPLE_CROSS 7 9 7 12 7 9 7 12
|
||||
LASER_BEAM_DOUBLE_TRIPLE_CROSS 7 10 7 13 7 10 7 13
|
Loading…
x
Reference in New Issue
Block a user