Commit daec45a6 authored by nargessalehi98's avatar nargessalehi98

Add GameState class

parent 2133eef4
111111111111111111111
101010100000001010111
101010100000001010111
101111111111111111111
111011100000000000001
111111100000000000000
111111111111111111111
010111000001100011000101110110001
000110111101110111001110001011111
011101000001111111110010010101100
001111000011011010111100010100110
010001001010100010001011100101001
100000110011011011011001111010101
010110101101100110001101111110010
001001001001111111111011101101011
100011101010010100011001000110010
100000100100111101101010010001111
100111101010110001101100110110000
111010100100110101100000101010001
001010000101101010000100111110110
101101001110011101000001011100111
110111100111010100111001101100001
000000111100110100011010011001100
000100011101010111110001000010101
111000101111010000100010100011000
101010101000000000101001010000001
101001010101000001010100010100111
111111010101010100100000000000101
\ No newline at end of file
111101000001000111111010110010011
101111000110000100110010110101110
100000001111111110010101001110000
011001100101011000100011111110000
000010000011010011000101110101001
011001000011001111110101010011110
010110100011100010011010011111000
000011111110100000101001010010011
000101001100010001010000100100100
011111100101000011101101110011010
100011011010101110010110100001100
111100001000100100111100010110001
100111111010011011010000010010001
101111010101010000001111000111110
000000110111101000000011101100010
100110010000110111011101100001010
111100110010011101000111001100001
001001011110100001001000011110010
\ No newline at end of file
011010001010111011100100010100010
001111111000110100001011011001011
101101001010111000111110101010110
101001110100001111001001011000111
011101001110111111100000101001011
010011011101001101111010010001000
010001000001001010001000101001110
010111011111000011001100101100010
010001001001100001100011111001011
010111111000010001001100100011100
111001011100110001000101100001011
111111100100100001100001001100100
101100110110000001111001011110010
110110101011010011111111100011111
110101010000101101111001101011010
011111111010011010011010111100110
000110110001010010111110011110110
101111111110110011101111010000011
\ No newline at end of file
100001101100101100010010001000000
100101010101100001011010000000001
001001110000001100101010110101111
101000110111111010000001101010011
100010111111011110011001011111010
111101111100111110100101100000001
001010001110000011011000100000101
010101010001110111100110000011011
011010010110111111001101100110111
101010010101111101000000011010000
010000100000100011001001101100100
000111000001100101110001011011100
101011011111010111011100100001100
000001001101100110111111111111000
111100001110101011010001111110000
101010101011010011111101011010011
101010100111111100101101010111010
011111001101111000011000101100011
\ No newline at end of file
010110001001000010010100011101111
110111110100001001000110100010010
010010010001010000111011010111000
010100000111011010110001110011111
010000110110001101110100000011011
100000101011111010001011010010100
101000011111001101011011001100000
101010110111010111100100010101001
000101010111111111011110010010101
000111111001111011110101100111010
011011010110000100110110101000101
000000001111111011000011111011000
001001111010001110111010001001101
110011001001110110100001110011001
111101011000110000010101001100011
101100111101110111101010100101100
001001000101010101111101010101100
000000001011011011011000000100100
\ No newline at end of file
/*** In The Name of Allah ***/
import java.awt.*;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.MouseMotionListener;
import java.awt.geom.AffineTransform;
import java.awt.image.BufferedImage;
/**
* This class holds the state of game and all of its elements.
* This class also handles user inputs, which affect the game state.
*
* @author Seyed Mohammad Ghaffarian
*/
public class GameState {
public int locX, locY, diam;
public boolean gameOver;
private boolean keyUP, keyDOWN, keyRIGHT, keyLEFT;
private boolean mousePress;
private int mouseX, mouseY;
private KeyHandler keyHandler;
private MouseHandler mouseHandler;
public double rotateAmount;
public GameState() {
locX = 100;
locY = 100;
diam = 42;
rotateAmount = 0;
gameOver = false;
//
keyUP = false;
keyDOWN = false;
keyRIGHT = false;
keyLEFT = false;
//
mousePress = false;
mouseX = 0;
mouseY = 0;
//
keyHandler = new KeyHandler();
mouseHandler = new MouseHandler();
}
/**
* The method which updates the game state.
*/
public void update() {
if (rotateAmount >= 360 || rotateAmount <= -360) rotateAmount = 0;
if (mousePress) {
locY = mouseY - diam / 2;
locX = mouseX - diam / 2;
}
if (keyUP) {
move(+5);
}
if (keyDOWN) {
move(-5);
}
if (keyLEFT)
rotateAmount -= 10;
if (keyRIGHT)
rotateAmount += 10;
locX = Math.max(locX, 42 );
locX = Math.min(locX, GameFrame.GAME_WIDTH - diam -160);
locY = Math.max(locY, 50);
locY = Math.min(locY, GameFrame.GAME_HEIGHT - diam-160);
}
public void move(int px) {
double d;
if (rotateAmount == 0) locX += px;
if (rotateAmount == 90 || rotateAmount == -270) locY += px;
if (rotateAmount == -90 || rotateAmount == 270) locY -= px;
if (rotateAmount == 180 || rotateAmount == -180) locX -= px;
if ((rotateAmount > 0 && rotateAmount < 90) || (rotateAmount < -270 && rotateAmount > -360)) {
if (px > 0) {
locX += px * Math.cos(rotateAmount * Math.PI / 180);
locY += px * Math.sin(rotateAmount * Math.PI / 180);
} else {
locX += px * Math.cos(rotateAmount * Math.PI / 180);
locY += px * Math.sin(rotateAmount * Math.PI / 180);
}
}
if ((rotateAmount > 90 && rotateAmount < 180) || (rotateAmount < -180 && rotateAmount > -270)) {
if (px > 0) {
d = rotateAmount - 90;
locX -= px * Math.sin(d * Math.PI / 180);
locY += px * Math.cos(d * Math.PI / 180);
} else {
d = rotateAmount + 270;
locX -= px * Math.sin(d * Math.PI / 180);
locY += px * Math.cos(d * Math.PI / 180);
}
}
if ((rotateAmount > 180 && rotateAmount < 270) || (rotateAmount < -90 && rotateAmount > -180)) {
if (px > 0) {
d = rotateAmount - 180;
locX -= px * Math.cos(d * Math.PI / 180);
locY -= px * Math.sin(d * Math.PI / 180);
} else {
d = rotateAmount + 180;
locX -= px * Math.cos(d * Math.PI / 180);
locY -= px * Math.sin(d * Math.PI / 180);
}
}
if ((rotateAmount > 270 && rotateAmount < 360) || (rotateAmount < 0 && rotateAmount > -90)) {
if (px > 0) {
d = rotateAmount - 270;
locX += px * Math.sin(d * Math.PI / 180);
locY -= px * Math.cos(d * Math.PI / 180);
} else {
d = rotateAmount + 90;
locX += px * Math.sin(d * Math.PI / 180);
locY -= px * Math.cos(d * Math.PI / 180);
}
}
}
public KeyListener getKeyListener() {
return keyHandler;
}
public MouseListener getMouseListener() {
return mouseHandler;
}
public MouseMotionListener getMouseMotionListener() {
return mouseHandler;
}
/**
* The keyboard handler.
*/
class KeyHandler extends KeyAdapter {
@Override
public void keyPressed(KeyEvent e) {
switch (e.getKeyCode()) {
case KeyEvent.VK_UP:
keyUP = true;
break;
case KeyEvent.VK_DOWN:
keyDOWN = true;
break;
case KeyEvent.VK_LEFT:
keyLEFT = true;
break;
case KeyEvent.VK_RIGHT:
keyRIGHT = true;
break;
case KeyEvent.VK_ESCAPE:
gameOver = true;
break;
}
}
@Override
public void keyReleased(KeyEvent e) {
switch (e.getKeyCode()) {
case KeyEvent.VK_UP:
keyUP = false;
break;
case KeyEvent.VK_DOWN:
keyDOWN = false;
break;
case KeyEvent.VK_LEFT:
keyLEFT = false;
break;
case KeyEvent.VK_RIGHT:
keyRIGHT = false;
break;
}
}
}
/**
* The mouse handler.
*/
class MouseHandler extends MouseAdapter {
@Override
public void mousePressed(MouseEvent e) {
mouseX = e.getX();
mouseY = e.getY();
mousePress = true;
}
@Override
public void mouseReleased(MouseEvent e) {
mousePress = false;
}
@Override
public void mouseDragged(MouseEvent e) {
mouseX = e.getX();
mouseY = e.getY();
}
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment