game logic fixed by tips of johan

This commit is contained in:
Remco
2015-06-04 13:20:46 +02:00
parent 76a41dbbd1
commit 1bf525c6b7
16 changed files with 266 additions and 263 deletions
+18 -1
View File
@@ -1,5 +1,10 @@
package control;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.Timer;
import model.GameModel;
import view.GameView;
import control.button.ButtonEvent;
@@ -7,17 +12,21 @@ import control.button.ButtonListener;
import control.joystick.JoystickEvent;
import control.joystick.JoystickListener;
public class GameControl implements JoystickListener, ButtonListener {
public class GameControl implements JoystickListener, ButtonListener,ActionListener {
private long lastTime = System.currentTimeMillis();
GameModel model;
GameView view;
GameStateManager gsm;
Timer update;
public GameControl(GameModel model, GameView view,GameStateManager gsm)
{
this.model = model;
this.view = view;
this.gsm = gsm;
update = new Timer(0, this);
update.start();
}
@Override
@@ -36,4 +45,12 @@ public class GameControl implements JoystickListener, ButtonListener {
public void onJoystickMoved(JoystickEvent e) {
gsm.currentState.onJoystickMoved(e);
}
@Override
public void actionPerformed(ActionEvent e) {
long currentTime = System.currentTimeMillis();
model.update(currentTime - lastTime);
lastTime = currentTime;
view.repaint();
}
}
+2 -2
View File
@@ -45,7 +45,7 @@ public class GameStateManager {
currentState.init();
}
public void update(){
currentState.update();
public void update(float factor){
currentState.update(factor);
}
}
+5 -15
View File
@@ -1,38 +1,28 @@
package model;
import java.awt.Color;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.Timer;
import control.GameStateManager;
import control.button.ButtonHandler;
public class GameModel implements ActionListener{
public class GameModel{
private Timer update;
public static Color[] colors = {Color.MAGENTA,Color.RED,Color.GREEN,Color.YELLOW,Color.CYAN,Color.WHITE};
private GameStateManager gsm;
SongHandler sh;
private SongHandler sh;
public GameModel(SongHandler sh, GameStateManager gsm)
{
this.gsm = gsm;
this.sh = sh;
update = new Timer(1000/30, this);
update.start();
this.sh = sh;
for(int i = 1; i < ButtonHandler.getButtons().size(); i++){
ButtonHandler.getButtons().get(i).setColor(colors[i-1]);;
}
}
@Override
public void actionPerformed(ActionEvent e) {
gsm.update();
public void update(float factor){
gsm.update(factor);
}
}
+1 -1
View File
@@ -28,7 +28,7 @@ public class SongHandler {
currentSong = null;
currentSongInstance = null;
currentIndex = 0;
currentIndex = 1;
p = new AudioPlayer();
+1 -1
View File
@@ -23,5 +23,5 @@ public abstract class DrawObject {
public abstract void draw(Graphics2D g2);
public abstract void update();
public abstract void update(float factor);
}
+189
View File
@@ -0,0 +1,189 @@
package model.drawObjects;
import java.awt.Color;
import java.awt.Graphics2D;
import java.awt.geom.Line2D;
import model.objects.Path;
public class Enemy extends DrawObject {
public static final int distanceToOctagon = 1000;
private final double secondsToEnd = 3;
private int length,timeToClick = 10,currentTimeToClick;
private double lengthOf1Side,distanceFromStart;//lengthOf1Side wordt alleen gebruikt als de lijn een schuine lijn is.
public Line2D enemy;
private Color c;
private boolean clickable = false;
private Path path;
public Enemy(int pathID,int lengthOfEnemy,Color c,Path path){
super();
this.length = lengthOfEnemy;
// System.out.println(this.length*this.length/2);
// System.out.println(Math.sqrt(Math.pow(this.length,2)/2));
lengthOf1Side = Math.sqrt(Math.pow(lengthOfEnemy,2)/2);
this.c = c;
this.index = pathID;
this.path = path;
double beginX, beginY,endX = 0,endY = 0;
beginX = path.getX1();
beginY = path.getY1();
//the 8 richtingen van de octagon
switch(index){
case 0:
endY -= this.length;
break;
case 1:
endY -= lengthOf1Side;
endX += lengthOf1Side;
break;
case 2:
endX -= this.length;
break;
case 3:
endY += lengthOf1Side;
endX += lengthOf1Side;
break;
case 4:
endY += this.length;
break;
case 5:
endY += lengthOf1Side;
endX -= lengthOf1Side;
break;
case 6:
endX -= this.length;
break;
case 7:
endY -= lengthOf1Side;
endX -= lengthOf1Side;
break;
}
endX += beginX;
endY += beginY;
enemy = new Line2D.Double(beginX, beginY, endX, endY);
}
@Override
public void draw(Graphics2D g2) {
g2.setPaint(c);
g2.draw(enemy);
}
@Override
public void update(float factor) {
if(clickable){
currentTimeToClick++;
}
distanceFromStart += (distanceToOctagon/(secondsToEnd*1000))*factor;
double x1,x2,y1,y2;
x1 = enemy.getX1();
x2 = enemy.getX2();
y1 = enemy.getY1();
y2 = enemy.getY2();
double angleX,angleY;
angleX = Math.cos(Math.toRadians(45))*distanceFromStart;
angleY = Math.sin(Math.toRadians(45))*distanceFromStart;
System.out.println(Math.toRadians(45)*distanceFromStart);
System.out.println(angleX+" - "+angleY);
switch(index){
case 0:
y1 = path.getY1() + distanceFromStart;
y2 = y1 - length;
break;
case 1:
// x1 -= speedX;
x1 = path.getX1() - angleX;
x2 = x1 + lengthOf1Side;
y1 = path.getY1() + angleY;
y2 = y1 - lengthOf1Side;
break;
case 2:
x1 = path.getX1()-distanceFromStart;
x2 = x1 + length;
break;
case 3:
x1 = path.getX1() - angleX;
x2 = x1 + lengthOf1Side;
y1 = path.getY1() - angleY;
y2 = y1 + lengthOf1Side;
break;
case 4:
y1 = path.getY1() - distanceFromStart;
y2 = y1 + length;
break;
case 5:
x1 = path.getX1() + angleX;
x2 = x1 - lengthOf1Side;
y1 = path.getY1() - angleY;
y2 = y1 + lengthOf1Side;
break;
case 6:
x1 = path.getX1() + distanceFromStart;
x2 = x1 - length;
break;
case 7:
x1 = path.getX1() + angleX;
x2 = x1 - lengthOf1Side;
y1 = path.getY1() + angleY;
y2 = y1 - lengthOf1Side;
break;
}
enemy.setLine(x1, y1, x2, y2);
}
public Color getColor() {
return c;
}
public void setColor(Color c) {
this.c = c;
}
public Line2D getEnemy() {
return enemy;
}
public void setBullet(Line2D bullet) {
this.enemy = bullet;
}
public void clickable(){
clickable = true;
}
/**
* deze methode kijkt hoeveel frame je nog hebt om te klikken op het moment dat je bolletje in de hitzone zit
* @return
*/
public boolean finised(){
if(currentTimeToClick >= timeToClick){
return true;
}
return false;
}
public boolean isClickable(){
return clickable;
}
public int getTimeLeftToClick() {
return currentTimeToClick;
}
}
+1 -1
View File
@@ -27,7 +27,7 @@ public class Player extends DrawObject {
}
public void update(){
public void update(float update){
if(lastindex != index){
transform = new AffineTransform();
transform.rotate(Math.toRadians(index*45),middlePoint.getX(),middlePoint.getY());
-199
View File
@@ -1,199 +0,0 @@
package model.drawObjects.enemy;
import java.awt.Color;
import java.awt.Graphics2D;
import java.awt.geom.Line2D;
import java.awt.geom.Point2D;
import model.drawObjects.DrawObject;
public class Enemy extends DrawObject {
private int length,timeToClick = 10,currentTimeToClick;
private double speedX,speedY,lengthOf1Side;//lengthOf1Side wordt alleen gebruikt als de lijn een schuine lijn is.
public Line2D enemy;
private Color c;
private boolean clickable = false;
/**
*
* @param pathID
* @param lengthOfEnemy
* @param c
* @param beginPoint
* @param endPoint
* @param steptsToEndPoint
*/
public Enemy(int pathID,int lengthOfEnemy,Color c,double steptsToEndPoint,Point2D beginPoint,Point2D endPoint){
super();
this.length = lengthOfEnemy;
// System.out.println(this.length*this.length/2);
// System.out.println(Math.sqrt(Math.pow(this.length,2)/2));
lengthOf1Side = Math.sqrt(Math.pow(lengthOfEnemy,2)/2);
this.c = c;
this.index = pathID;
double beginX, beginY,endX = 0,endY = 0;
beginX = beginPoint.getX();
beginY = beginPoint.getY();
endX = beginX;
endY = beginY;
//the 8 richtingen van de octagon
switch(index){
case 0:
endY -= this.length;
break;
case 1:
endY -= lengthOf1Side;
endX += lengthOf1Side;
break;
case 2:
endX -= this.length;
break;
case 3:
endY += lengthOf1Side;
endX += lengthOf1Side;
break;
case 4:
endY += this.length;
break;
case 5:
endY += lengthOf1Side;
endX -= lengthOf1Side;
break;
case 6:
endX += this.length;
break;
case 7:
endY -= lengthOf1Side;
endX -= lengthOf1Side;
break;
}
enemy = new Line2D.Double(beginX, beginY, endX, endY);
//bereken de lengte van de enemy tot het einde van de lijn
double distanceX,distanceY;
distanceX = (enemy.getX1() - endPoint.getX());
distanceY = (enemy.getY1() - endPoint.getY());
//de afstand mag niet negatief zijn
if(distanceX < 0){
distanceX *= -1;
}
if(distanceY < 0){
distanceY *= -1;
}
// System.out.println("Index: "+index+"\tX afstand: "+distanceX+"\tY afstand: "+distanceY);
//snelheid = afstand /tijd
speedX = (distanceX /steptsToEndPoint);
speedY = (distanceY /steptsToEndPoint);
// System.out.println("Index: "+index+"\tX speed: "+speedX+"\tY speed: "+speedY);
}
@Override
public void draw(Graphics2D g2) {
g2.setPaint(c);
g2.draw(enemy);
}
@Override
public void update() {
if(clickable){
currentTimeToClick++;
}
double x1,x2,y1,y2;
x1 = enemy.getX1();
x2 = enemy.getX2();
y1 = enemy.getY1();
y2 = enemy.getY2();
switch(index){
case 0:
y1 += speedY;
y2 = y1 - length;
break;
case 1:
x1 -= speedX;
x2 = x1 + lengthOf1Side;
y1 += speedY;
y2 = y1 - lengthOf1Side;
break;
case 2:
x1 -= speedX;
x2 = x1 + length;
break;
case 3:
x1 -= speedX;
x2 = x1 + lengthOf1Side;
y1 -= speedY;
y2 = y1 + lengthOf1Side;
break;
case 4:
y1 -= speedY;
y2 = y1 + length;
break;
case 5:
x1 += speedX;
x2 = x1 - lengthOf1Side;
y1 -= speedY;
y2 = y1 + lengthOf1Side;
break;
case 6:
x1 += speedX;
x2 = x1 - length;
break;
case 7:
x1 += speedX;
x2 = x1 - lengthOf1Side;
y1 += speedY;
y2 = y1 - lengthOf1Side;
break;
}
enemy.setLine(x1, y1, x2, y2);
}
public Color getColor() {
return c;
}
public void setColor(Color c) {
this.c = c;
}
public Line2D getEnemy() {
return enemy;
}
public void setBullet(Line2D bullet) {
this.enemy = bullet;
}
public void clickable(){
clickable = true;
}
/**
* deze methode kijkt hoeveel frame je nog hebt om te klikken op het moment dat je bolletje in de hitzone zit
* @return
*/
public boolean finised(){
if(currentTimeToClick >= timeToClick){
return true;
}
return false;
}
public boolean isClickable(){
return clickable;
}
public int getTimeLeftToClick() {
return currentTimeToClick;
}
}
+1 -1
View File
@@ -18,7 +18,7 @@ public abstract class GameState {
}
public abstract void init();
public abstract void update();
public abstract void update(float factor);
public abstract void draw(Graphics2D g2);
public abstract void buttonPressed(ButtonEvent e);
public abstract void buttonReleased(ButtonEvent e);
+1 -1
View File
@@ -39,7 +39,7 @@ public class MenuState extends GameState {
}
@Override
public void update() {
public void update(float factor) {
buttonInAnimation(animationcounter);
for(MenuButton b:buttons){
b.update();
+1 -4
View File
@@ -1,9 +1,6 @@
package model.gameState;
import java.awt.Color;
import java.awt.Graphics2D;
import java.awt.Window;
import java.awt.image.BufferedImage;
import model.SongHandler;
import control.GameStateManager;
@@ -23,7 +20,7 @@ public class PickSongState extends GameState {
}
@Override
public void update() {
public void update(float factor) {
// TODO Auto-generated method stub
}
+18 -18
View File
@@ -11,8 +11,8 @@ import java.util.Iterator;
import model.GameModel;
import model.SongHandler;
import model.drawObjects.Enemy;
import model.drawObjects.Player;
import model.drawObjects.enemy.Enemy;
import model.objects.InfoPanel;
import model.objects.Path;
import model.objects.PlayArea;
@@ -39,9 +39,9 @@ public class PlayState extends GameState{
super(gsm,sh);
infoPanel = new InfoPanel(0, 0);
area = new PlayArea(256, 1024, 1024, 100);
// for(int index = 0; index < 8; index++){
// addEnemy(index, GameModel.colors[index % 6]);
// }
for(int index = 0; index < 8; index++){
addEnemy(index, GameModel.colors[index % 6]);
}
player = new Player(1280-1024+1024/2, 1024/2);
stroke = new BasicStroke(sizeOfEnemy,BasicStroke.CAP_ROUND,BasicStroke.JOIN_ROUND);
@@ -52,12 +52,12 @@ public class PlayState extends GameState{
sh.stop();
sh.play();
System.out.println("Diff" + sh.getCurrentSongInstance().getDifficulty());
// System.out.println("Diff" + sh.getCurrentSongInstance().getDifficulty());
}
@Override
public void update() {
player.update();
public void update(float factor) {
player.update(factor);
for(Path path : area.paths){
Iterator<Enemy>enemyIterator = path.getEnemysInPath().iterator();
while(enemyIterator.hasNext()){
@@ -69,21 +69,21 @@ public class PlayState extends GameState{
if(hitArea.intersectsLine(e.enemy) && !e.isClickable()){
e.clickable();
}
e.update();
e.update(factor);
}
}
infoPanel.updateIPanel();
long progress = sh.getProgress() / 1000;
for(ObjectInstance ob : sh.getCurrentSongInstance().getBetween(oldProgress, progress))
{
Path p = area.paths.get(ob.getDirection());
p.addEnemy(ob.getColor(), ob.getDirection(), (int) ob.getLength());
}
oldProgress = progress;
// long progress = sh.getProgress() / 1000;
//
// for(ObjectInstance ob : sh.getCurrentSongInstance().getBetween(oldProgress, progress))
// {
// Path p = area.paths.get(ob.getDirection());
// p.addEnemy(ob.getColor(), ob.getDirection(), (int) ob.getLength());
// }
//
// oldProgress = progress;
}
@@ -166,7 +166,7 @@ public class PlayState extends GameState{
public void addEnemy(int pathID,Color color){
Path path = area.paths.get(pathID);
Enemy e = new Enemy(pathID,1,color,100,path.getP1(),path.getP2());
Enemy e = new Enemy(pathID,1,color,path);
path.getEnemysInPath().addLast(e);
}
}
+1 -1
View File
@@ -34,7 +34,7 @@ public class TitleState extends GameState {
}
@Override
public void update() {
public void update(float factor) {
frame++;
}
+2 -2
View File
@@ -4,7 +4,7 @@ import java.awt.Color;
import java.awt.geom.Line2D;
import java.util.ArrayDeque;
import model.drawObjects.enemy.Enemy;
import model.drawObjects.Enemy;
public class Path extends Line2D.Double {
@@ -29,6 +29,6 @@ public class Path extends Line2D.Double {
public void addEnemy(Color c, int pathID, int length)
{
enemysInPath.addLast(new Enemy(pathID, length, c, 100, getP1(), getP2()));
enemysInPath.addLast(new Enemy(pathID, length, c,this));
}
}
+22 -8
View File
@@ -8,6 +8,7 @@ import java.awt.geom.Rectangle2D;
import java.util.ArrayList;
import java.util.List;
import model.drawObjects.Enemy;
import model.gameState.PlayState;
public class PlayArea {
@@ -67,14 +68,27 @@ public class PlayArea {
widthOfGameScreen += xToRight;
paths.add(new Path(middlePointX,0 ,hitArea.xpoints[6],hitArea.ypoints[6]));//top
paths.add(new Path(widthOfGameScreen,0 ,hitArea.xpoints[7],hitArea.ypoints[7]));//right -top
paths.add(new Path(widthOfGameScreen,middlePointY ,hitArea.xpoints[0],hitArea.ypoints[0]));//right
paths.add(new Path(widthOfGameScreen,heightOfGameScreen,hitArea.xpoints[1],hitArea.ypoints[1]));//right -down
paths.add(new Path(middlePointX,heightOfGameScreen ,hitArea.xpoints[2],hitArea.ypoints[2]));//down
paths.add(new Path(xToRight,heightOfGameScreen ,hitArea.xpoints[3],hitArea.ypoints[3]));//left -down
paths.add(new Path(xToRight,middlePointY ,hitArea.xpoints[4],hitArea.ypoints[4]));//left
paths.add(new Path(xToRight,0 ,hitArea.xpoints[5],hitArea.ypoints[5]));//left -top
// paths.add(new Path(middlePointX,0 ,hitArea.xpoints[6],hitArea.ypoints[6]));//top
// paths.add(new Path(widthOfGameScreen,0 ,hitArea.xpoints[7],hitArea.ypoints[7]));//right -top
// paths.add(new Path(widthOfGameScreen,middlePointY ,hitArea.xpoints[0],hitArea.ypoints[0]));//right
// paths.add(new Path(widthOfGameScreen,heightOfGameScreen,hitArea.xpoints[1],hitArea.ypoints[1]));//right -down
// paths.add(new Path(middlePointX,heightOfGameScreen ,hitArea.xpoints[2],hitArea.ypoints[2]));//down
// paths.add(new Path(xToRight,heightOfGameScreen ,hitArea.xpoints[3],hitArea.ypoints[3]));//left -down
// paths.add(new Path(xToRight,middlePointY ,hitArea.xpoints[4],hitArea.ypoints[4]));//left
// paths.add(new Path(xToRight,0 ,hitArea.xpoints[5],hitArea.ypoints[5]));//left -top
double angleX,angleY;
angleX = (Math.cos(Math.toRadians(45)))*Enemy.distanceToOctagon;
angleY = (Math.sin(Math.toRadians(45)))*Enemy.distanceToOctagon;
paths.add(new Path(hitArea.xpoints[6],hitArea.ypoints[6]-Enemy.distanceToOctagon ,hitArea.xpoints[6],hitArea.ypoints[6]));//top
paths.add(new Path(hitArea.xpoints[7] + angleX,hitArea.ypoints[7] - angleY ,hitArea.xpoints[7],hitArea.ypoints[7]));//right -top
paths.add(new Path(hitArea.xpoints[0]+Enemy.distanceToOctagon,hitArea.ypoints[0] ,hitArea.xpoints[0],hitArea.ypoints[0]));//right
paths.add(new Path(hitArea.xpoints[1]+angleX,hitArea.ypoints[1]+angleY ,hitArea.xpoints[1],hitArea.ypoints[1]));//right -down
paths.add(new Path(hitArea.xpoints[2],hitArea.ypoints[2]+Enemy.distanceToOctagon ,hitArea.xpoints[2],hitArea.ypoints[2]));//down
paths.add(new Path(hitArea.xpoints[3] - angleX,hitArea.ypoints[3] + angleY ,hitArea.xpoints[3],hitArea.ypoints[3]));//left -down
paths.add(new Path(hitArea.xpoints[4] - Enemy.distanceToOctagon,hitArea.ypoints[4] ,hitArea.xpoints[4],hitArea.ypoints[4]));//left
paths.add(new Path(hitArea.xpoints[5] - angleX,hitArea.ypoints[5] - angleY ,hitArea.xpoints[5],hitArea.ypoints[5]));//left -top
}
+3 -8
View File
@@ -13,14 +13,14 @@ import javax.swing.Timer;
import control.GameStateManager;
import control.LedHandler;
public class GameView extends JPanel implements ActionListener{
public class GameView extends JPanel{
/**
*
*/
private static final long serialVersionUID = 1939480784205689618L;
Timer t;
LedHandler led;
GameStateManager gsm;
@@ -28,15 +28,10 @@ public class GameView extends JPanel implements ActionListener{
{
this.led=led;
this.gsm = gsm;
t = new Timer(1000/60, this);
t.start();
setPreferredSize(new Dimension(1280,1024));
}
public void actionPerformed(ActionEvent arg0) {
repaint();
}
@Override
public void paintComponent(Graphics g)
{