Commit 24a8caf2 authored by 9731301's avatar 9731301

change wall structure

parent 05bedc2e
This diff is collapsed.
......@@ -4,4 +4,5 @@
100111000111011100001
101100000000000100111
100000111100111100001
100000010000010000001
111111111111111111111
\ No newline at end of file
1111111
1000001
1010001
1111111
\ No newline at end of file
11111
10101
10111
10101
11111
\ No newline at end of file
111111111
100100001
100100011
101100001
101000001
101101111
100100001
111111111
\ No newline at end of file
1111111111111
1001001000001
1001100010001
1011020011111
1000020010001
1011011110001
1001000000111
1111111111111
\ No newline at end of file
......@@ -9,14 +9,14 @@ import java.awt.event.KeyEvent;
public class BulletState {
public int locX, locY, diam , angle;
private WallState wallState;
private WallState walls;
private boolean keySpace;
private KeyHandler2 keyHandler2 = new KeyHandler2();
private long createTime;
public BulletState(WallState wallState , TankState tankState){
this.wallState = wallState ;
this.walls = wallState ;
// Initialize the game state and all elements ...
locX = tankState.locX;
locY = tankState.locY;
......@@ -30,6 +30,15 @@ public class BulletState {
public void update(){
locY += 10*Math.sin(Math.toRadians(angle) ) ;
locX += 10*Math.cos(Math.toRadians(angle) ) ;
for (WallState.WallData wall : walls.getWalls()){
if (wall.isWall(locX , locY) && wall.isHorizontal()) {
locY -= 10 * Math.sin(Math.toRadians(angle));
}
if (wall.isWall(locX , locY)&& wall.isVertical()){
locX-= 10*Math.cos(Math.toRadians(angle) ) ;
}
}
}
......
......@@ -71,7 +71,6 @@ public class TankState {
if (keyUP) {
if (rotate == 0)
rotate = 360;
System.out.println(rotate);
locY -= 8;
if ((rotate > 180 && rotate < 270)|| rotate ==180 )
rotate +=10;
......@@ -115,7 +114,6 @@ public class TankState {
if (bullets.size() == 0 || System.currentTimeMillis() -bullets.get(bullets.size()-1).getCreateTime()>500 ) {
BulletState newBullet = new BulletState(walls, this);
bullets.add(newBullet);
System.out.println(newBullet.angle);
}
}
......@@ -206,9 +204,6 @@ public class TankState {
}
public void setWalls(WallState walls) {
this.walls = walls;
}
/**
* The mouse handler.
......
package UI.GameState;
import javax.imageio.ImageIO;
import java.awt.*;
import java.io.File;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
......@@ -16,6 +18,9 @@ public class WallState {
private int numRows, numColumns, w, h;
private final int wallWidth = 5;
private ArrayList<WallData> walls = new ArrayList<>();
private Image wall1 ;
private Image wall2 ;
private Image blank ;
public WallState(int GAME_WIDTH, int GAME_HEIGHT) {
......@@ -24,6 +29,9 @@ public class WallState {
try {
source = new String(Files.readAllBytes(Paths.get("C:\\Users\\Lenovo\\IdeaProjects\\JTankTrouble\\maps\\map3.txt")), StandardCharsets.UTF_8);
wall1 = ImageIO.read(new File("C:\\Users\\Lenovo\\IdeaProjects\\JTankTrouble\\pics\\wall1.png"));
wall2 = ImageIO.read(new File("C:\\Users\\Lenovo\\IdeaProjects\\JTankTrouble\\pics\\wall2.png"));
blank = ImageIO.read(new File("C:\\Users\\Lenovo\\IdeaProjects\\JTankTrouble\\pics\\blank.png"));
} catch (IOException e) {
e.printStackTrace();
}
......@@ -34,18 +42,48 @@ public class WallState {
h = (frameY-30) / (numRows); //sideY
w = (frameX-10) / (numColumns); //sideX
}
public void paintComponent(Graphics g) {
Image wall1scaled = wall1.getScaledInstance(w, h, java.awt.Image.SCALE_SMOOTH);
Image wall2scaled = wall2.getScaledInstance(w, h, java.awt.Image.SCALE_SMOOTH);
Image blankScaled = blank.getScaledInstance(w, h, java.awt.Image.SCALE_SMOOTH);
// Draw background
walls.clear();
g.setColor(Color.WHITE);
g.fillRect(0, 0, frameX, frameY);
g.setColor(Color.black); //colour of the walls
int x = 0;
int y = 0;
for (int r = 0; r < numRows; r++) {
x = 0 ;
String rowChars = lines[r];
for (int c = 0; c < numColumns; c++) {
if (rowChars.charAt(c) == '0') {
drawBlanks(g, x, y ,blankScaled);
x += w;
}
else if (rowChars.charAt(c) == '1') {
drawAndSaveWalls(g, x, y, r , c, wall1scaled);
x += w;
}
else if (rowChars.charAt(c) == '2') {
drawAndSaveWalls(g, x, y, r , c, wall2scaled);
x += w;
}
}
y += h;
}
int start = 10;
/* int start = 10;
int startTop = 30;
for (int r = 0; r < numRows; r++) {
String rowChars = lines[r];
......@@ -98,12 +136,25 @@ public class WallState {
}
}
//draw frame
g.setColor(Color.WHITE);
g.fillRect(0,frameX,frameX,startTop);
g.fillRect(0,0,start,frameY);
g.fillRect(frameX-start,0,start,frameY);
g.fillRect(0,frameY-start,frameX,start);
*/
}
private void drawAndSaveWalls(Graphics g2d,int x,int y , int r , int c ,Image image){
WallData wall = new WallData(x , y , w, h);
if ((c<numColumns-1 && lines[r].charAt(c+1)=='1') || (c>0 && lines[r].charAt(c-1)=='1')){ //horizontal
wall.setHorizontal(true);
}
if ((r>0 && lines[r-1].charAt(c)=='1') || (r>0 && r<numRows-1 && lines[r+1].charAt(c)=='1')) { //vertical
wall.setVertical(true);
}
walls.add(wall);
g2d.drawImage(image , x , y , null);
}
private void drawBlanks(Graphics g2d,int x,int y,Image image){
g2d.drawImage(image , x , y , null);
}
public boolean isWall(int x,int y){
......@@ -113,15 +164,15 @@ public class WallState {
return false;
}
private void draw(Graphics g,int x,int y,int w,int h){
walls.add(new WallData(x,y,w,h));
g.fillRect(x,y,w,h);
}
public ArrayList<WallData> getWalls() {
return walls;
}
public class WallData {
int x,y,w,h;
private int x,y,w,h;
private boolean horizontal = false;
private boolean vertical = false;
public WallData(int x,int y,int w,int h){
this.x = x;
......@@ -134,5 +185,21 @@ public class WallState {
//BUG
return (x>this.x && x<this.x+w && y>this.y && y<this.y+h);
}
public boolean isHorizontal() {
return horizontal;
}
public void setHorizontal(boolean horizontal) {
this.horizontal = horizontal;
}
public boolean isVertical() {
return vertical;
}
public void setVertical(boolean vertical) {
this.vertical = vertical;
}
}
}
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