Commit 24a8caf2 authored by 9731301's avatar 9731301

change wall structure

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