Working on moving steel

master
bob 2022-06-16 13:32:58 -04:00
parent 53145345a9
commit 149b689893
3 changed files with 50 additions and 18 deletions

View File

@ -41,6 +41,6 @@ public class FileManager {
FileOutputStream fileStream = new FileOutputStream(fileLocation);
ObjectOutputStream objectStream = new ObjectOutputStream(fileStream);
objectStream.writeObject(o);
System.out.println(o + " " + fileLocation);
//System.out.println(o + " " + fileLocation);
}
}

View File

@ -161,7 +161,7 @@ public class GamePanel extends JPanel implements Runnable, KeyListener, Serializ
//make this class run at the same time as other classes (without this each class would "pause" while another class runs). By using threading we can remove lag, and also allows us to do features like display timers in real time!
gameThread = new Thread(this);
gameThread.start();
System.out.println(gameThread);
//System.out.println(gameThread);
}
//paint is a method in java.awt library that we are overriding. It is a special method - it is called automatically in the background in order to update what appears in the window. You NEVER call paint() yourself

View File

@ -18,7 +18,7 @@ public class Player extends GenericSprite {
public static final int PLAYER_WIDTH = 52;
public static final int PLAYER_HEIGHT = 94;
public static final int steelReachRange = 3*Tile.length;
public static final int steelReachRange = 4*Tile.length;
public int lastXDirection, lastYDirection, lastFrame;
public static final int walkSpeedCap = 5;
@ -132,13 +132,35 @@ public class Player extends GenericSprite {
// return false;
// }
public void updatePlaceSteel(int x, int y){
if(this.y<0){
canPlaceSteel = false;
return;
}
canPlaceSteel = true;
boolean adjacent = false;
int realX = x*Tile.length-GameFrame.game.camera.x;
double xDis = (realX - GamePanel.GAME_WIDTH/2+ Tile.length/2) - (this.x+WIDTH/2);
double yDis = (y*Tile.length + Tile.length/2) - (this.y+HEIGHT/2);
double hypo = Math.sqrt(xDis*xDis+yDis*yDis);
int xx = (mouseX + GameFrame.game.camera.x + GamePanel.GAME_WIDTH / 2) / Tile.length;
int yy = (mouseY / Tile.length);
//System.out.println(hypo +" "+xDis + " "+ realX);
for(NonPlayer e: GameFrame.game.enemy){
System.out.println((xx*Tile.length)+" "+(yy*Tile.length)+" ");
System.out.print(" "+ (e.x+GamePanel.GAME_WIDTH/2) + " "+e.y+" ");
// if(){
//
// } else {
// canPlaceSteel = false;
// return;
// }
}
if(Math.abs(xDis)<(WIDTH+Tile.length)/2&&Math.abs(yDis)<(HEIGHT+Tile.length)/2){
canPlaceSteel = false; return;
}
int[][]check = {{1,0},{0,1},{-1,0},{0,-1}};
for(int[]a: check){
try{
@ -151,10 +173,13 @@ public class Player extends GenericSprite {
}
}
if(!adjacent){canPlaceSteel = false; return;}
if(hypo>3*Tile.length){canPlaceSteel = false; return;}
if(hypo>steelReachRange){canPlaceSteel = false; return;}
if(GameFrame.game.map[x][y]!=null&&!GameFrame.game.map[x][y].replaceAble){canPlaceSteel = false; return;}
;
if(!canReach(xx,yy)){canPlaceSteel = false; return;};
//System.out.println(realX);
}
public void move() throws IOException {
// mouseX = MouseInfo.getPointerInfo().getLocation().x;
// mouseY = MouseInfo.getPointerInfo().getLocation().y;
@ -265,12 +290,13 @@ public class Player extends GenericSprite {
int x = (mouseX + GameFrame.game.camera.x + GamePanel.GAME_WIDTH / 2) / Tile.length;
int y = (mouseY / Tile.length);
rightMouseDown = true;
if (!holdingSteel) {
if (GameFrame.game.map[x][y] != null&&GameFrame.game.map[x][y].movable) {
double xDis = (this.x+WIDTH/2) - (GameFrame.game.map[x][y].realX+Tile.length/2);
double yDis = (this.y+HEIGHT/2) - (GameFrame.game.map[x][y].y+Tile.length/2);
double hypo = Math.sqrt(xDis*xDis+yDis*yDis);
if(hypo<steelReachRange) {
if (GameFrame.game.map[x][y] != null && GameFrame.game.map[x][y].movable) {
double xDis = (this.x + WIDTH / 2) - (GameFrame.game.map[x][y].realX + Tile.length / 2);
double yDis = (this.y + HEIGHT / 2) - (GameFrame.game.map[x][y].y + Tile.length / 2);
double hypo = Math.sqrt(xDis * xDis + yDis * yDis);
if (hypo <steelReachRange) {
holdingSteel = true;
if (GameFrame.game.map[x][y].previousBlock != null) {
GameFrame.game.map[x][y] = GameFrame.game.map[x][y].previousBlock;
@ -279,14 +305,15 @@ public class Player extends GenericSprite {
}
}
}
} else if((GameFrame.game.map[x][y] == null||GameFrame.game.map[x][y].replaceAble)&&canPlaceSteel){
} else if ((GameFrame.game.map[x][y] == null || GameFrame.game.map[x][y].replaceAble) && canPlaceSteel) {
Tile temp = GameFrame.game.map[x][y];
GameFrame.game.map[x][y] = new SingleTile(x*Tile.length - (GamePanel.GAME_WIDTH /2), y*Tile.length, new BufferedImageWrapper(("img/tiles/boxes/steel.png")));
GameFrame.game.map[x][y] = new SingleTile(x * Tile.length - (GamePanel.GAME_WIDTH / 2), y * Tile.length, new BufferedImageWrapper(("img/tiles/boxes/steel.png")));
GameFrame.game.map[x][y].movable = true;
GameFrame.game.map[x][y].previousBlock = temp;
holdingSteel = false;
}
}
}
}
@ -295,7 +322,9 @@ public class Player extends GenericSprite {
int pX = (int)(((double)GameFrame.game.camera.x + GamePanel.GAME_WIDTH) / Tile.length);
int pY = (int)(((double)this.y+HEIGHT/2)/Tile.length);
//System.out.println(pX+" "+pY);
if(pY<0){
return false;
}
//BFS
int[][]check = {{0,1},{1,0},{-1,0},{0,-1}};
int[][]dis = new int[1000][18];
@ -316,19 +345,22 @@ public class Player extends GenericSprite {
try{
int newX = tempX + a[0];
int newY = tempY + a[1];
if(dis[tempX][tempY]+1<(Math.min(3,dis[newX][newY]))){
if(dis[tempX][tempY]+1<(Math.min(6,dis[newX][newY]))) {
dis[newX][newY] = dis[tempX][tempY]+1;
xx.add(newX);
yy.add(newY);
if(GameFrame.game.map[newX][newY]==null||GameFrame.game.map[newX][newY].replaceAble) {
xx.add(newX);
yy.add(newY);
}
}
} catch(Exception e){
}
}
}
if(dis[pX][pY]<=3){
if(dis[x][y]<=6){
return true;
}
return false;
}
public void mouseDragged(MouseEvent e) {