Aufgaben 10

This commit is contained in:
Andreas Greiner 2021-06-07 16:23:33 +02:00
parent 5cae1734d7
commit 301c4993a7
5 changed files with 69 additions and 4 deletions

View File

@ -0,0 +1,48 @@
package de.thdeg.greiner.superpangworld.graphics.base;
import java.util.*;
/**
* Offers different movement patterns for moveable game objects.
*/
public class MovementPatterns {
private HashMap<String, ArrayList<Position>> patterns;
private Random random;
/**
* Create the MovementPatterns object providing access to different movement patterns.
*/
public MovementPatterns(){
patterns = new HashMap<>();
random = new Random();
ArrayList<Position> square = new ArrayList<>((Arrays.asList(new Position(30,30),new Position(930,30),
new Position(930,510),new Position(30,510))));
ArrayList<Position> zigZag = new ArrayList<>((Arrays.asList( new Position(300,200),new Position(400,340),new Position(500,200),
new Position(600,340),new Position(700,200),new Position(800,340))));
patterns.put("square",square);
patterns.put("zigzag",zigZag);
}
/**
* Get a movement pattern by its name.
* @param pattern the pattern name
* @return the movement pattern
*/
public ArrayList<Position> getPattern(String pattern){
return patterns.get(pattern);
}
/**
* get a random movement pattern.
* @return the movement pattern
*/
public ArrayList<Position> getRandomPattern(){
int index = random.nextInt(patterns.values().size());
return new ArrayList<ArrayList<Position>>(patterns.values()).get(index);
}
}

View File

@ -2,16 +2,22 @@ package de.thdeg.greiner.superpangworld.graphics.moveable;
import de.thdeg.greiner.superpangworld.gameview.GameView; import de.thdeg.greiner.superpangworld.gameview.GameView;
import de.thdeg.greiner.superpangworld.graphics.base.GameObject; import de.thdeg.greiner.superpangworld.graphics.base.GameObject;
import de.thdeg.greiner.superpangworld.graphics.base.MovementPatterns;
import de.thdeg.greiner.superpangworld.graphics.base.MovingGameObject; import de.thdeg.greiner.superpangworld.graphics.base.MovingGameObject;
import de.thdeg.greiner.superpangworld.graphics.base.Position; import de.thdeg.greiner.superpangworld.graphics.base.Position;
import java.awt.*; import java.awt.*;
import java.util.Random; import java.util.*;
import java.util.List;
/** Ball with random movement. */ /** Ball with random movement. */
public class RandomBall extends GameObject implements MovingGameObject { public class RandomBall extends GameObject implements MovingGameObject {
private final Position targetPosition;
private final Random random; private final Random random;
private ArrayList<Position> movementPattern;
private int movementPatternIndex;
private Position targetPosition;
private MovementPatterns movementPatterns;
/** /**
* Creates the GameObject with the GameView to be displayed on. * Creates the GameObject with the GameView to be displayed on.
@ -20,10 +26,15 @@ public class RandomBall extends GameObject implements MovingGameObject {
*/ */
public RandomBall(GameView gameView) { public RandomBall(GameView gameView) {
super(gameView); super(gameView);
this.targetPosition = new Position(800, 200);
this.random = new Random(); this.random = new Random();
this.size = 50; this.size = 50;
this.speedInPixel = 4; this.speedInPixel = 4;
movementPatterns = new MovementPatterns();
movementPattern = movementPatterns.getRandomPattern();
movementPatternIndex = 0;
targetPosition = movementPattern.get(0);
} }
@Override @Override
@ -33,7 +44,13 @@ public class RandomBall extends GameObject implements MovingGameObject {
position.right((targetPosition.x - position.x) / distance * speedInPixel); position.right((targetPosition.x - position.x) / distance * speedInPixel);
position.down((targetPosition.y - position.y) / distance * speedInPixel); position.down((targetPosition.y - position.y) / distance * speedInPixel);
} else { } else {
setRandomTargetPosition(); if(movementPatternIndex < movementPattern.size()-1){
movementPatternIndex++;
}else{
movementPattern = movementPatterns.getRandomPattern();
movementPatternIndex = 0;
}
targetPosition = movementPattern.get(movementPatternIndex);
} }
} }