Commit 90315b7f authored by 9731301's avatar 9731301

complete client networkPlaying

parent 3df333c1
......@@ -7,17 +7,11 @@
</component>
<component name="ChangeListManager">
<list default="true" id="0b3d586d-9f0f-4ca4-b09d-03a200e1e684" name="Default Changelist" comment="">
<change afterPath="$PROJECT_DIR$/.idea/artifacts/JTankTrouble_jar.xml" afterDir="false" />
<change afterPath="$PROJECT_DIR$/src/Server_Client/NetWorkGame/Client1.java" afterDir="false" />
<change afterPath="$PROJECT_DIR$/src/UI/LogIn/NetWorkGame/NWGFrame.java" afterDir="false" />
<change afterPath="$PROJECT_DIR$/src/UI/LogIn/NetWorkGame/NWGLoop.java" afterDir="false" />
<change afterPath="$PROJECT_DIR$/src/UI/LogIn/NetWorkGame/NWGRendering.java" afterDir="false" />
<change beforePath="$PROJECT_DIR$/.idea/workspace.xml" beforeDir="false" afterPath="$PROJECT_DIR$/.idea/workspace.xml" afterDir="false" />
<change beforePath="$PROJECT_DIR$/out/production/JTankTrouble/UI/LogIn/MainPanel$1.class" beforeDir="false" />
<change beforePath="$PROJECT_DIR$/out/production/JTankTrouble/UI/LogIn/MainPanel$2.class" beforeDir="false" />
<change beforePath="$PROJECT_DIR$/out/production/JTankTrouble/UI/LogIn/MainPanel$3.class" beforeDir="false" />
<change beforePath="$PROJECT_DIR$/out/production/JTankTrouble/UI/LogIn/MainPanel.class" beforeDir="false" />
<change beforePath="$PROJECT_DIR$/src/UI/LogIn/MainPanel.java" beforeDir="false" afterPath="$PROJECT_DIR$/src/UI/LogIn/MainPanel.java" afterDir="false" />
<change beforePath="$PROJECT_DIR$/src/Server_Client/NetWorkGame/Client1.java" beforeDir="false" afterPath="$PROJECT_DIR$/src/Server_Client/NetWorkGame/Client1.java" afterDir="false" />
<change beforePath="$PROJECT_DIR$/src/UI/GameState/WallState.java" beforeDir="false" afterPath="$PROJECT_DIR$/src/UI/GameState/WallState.java" afterDir="false" />
<change beforePath="$PROJECT_DIR$/src/UI/LogIn/NetWorkGame/NWGLoop.java" beforeDir="false" afterPath="$PROJECT_DIR$/src/UI/LogIn/NetWorkGame/NWGLoop.java" afterDir="false" />
<change beforePath="$PROJECT_DIR$/src/UI/LogIn/NetWorkGame/NWGRendering.java" beforeDir="false" afterPath="$PROJECT_DIR$/src/UI/LogIn/NetWorkGame/NWGRendering.java" afterDir="false" />
</list>
<option name="SHOW_DIALOG" value="false" />
<option name="HIGHLIGHT_CONFLICTS" value="true" />
......@@ -156,6 +150,8 @@
<workItem from="1596559015419" duration="8215000" />
<workItem from="1596589736833" duration="13294000" />
<workItem from="1596613090214" duration="8123000" />
<workItem from="1596634146159" duration="2212000" />
<workItem from="1596700092093" duration="11265000" />
</task>
<servers />
</component>
......@@ -167,10 +163,10 @@
<screen x="0" y="0" width="1536" height="824" />
</state>
<state x="517" y="216" key="#com.intellij.fileTypes.FileTypeChooser/0.0.1536.824@0.0.1536.824" timestamp="1596402386433" />
<state x="574" y="122" key="#com.intellij.ide.util.MemberChooser" timestamp="1596620195172">
<state x="574" y="122" key="#com.intellij.ide.util.MemberChooser" timestamp="1596713317284">
<screen x="0" y="0" width="1536" height="824" />
</state>
<state x="574" y="122" key="#com.intellij.ide.util.MemberChooser/0.0.1536.824@0.0.1536.824" timestamp="1596620195172" />
<state x="574" y="122" key="#com.intellij.ide.util.MemberChooser/0.0.1536.824@0.0.1536.824" timestamp="1596713317284" />
<state x="499" y="197" key="#com.intellij.ide.util.TreeClassChooserDialog" timestamp="1596591250036">
<screen x="0" y="0" width="1536" height="824" />
</state>
......
package Server_Client.NetWorkGame;
import UI.GameState.BulletState;
import UI.GameState.EnemyTank;
import UI.GameState.TankState;
import UI.GameState.WallState;
import UI.GameState.*;
import java.io.IOException;
import java.io.InputStream;
......@@ -16,32 +13,21 @@ public class Client1 {
private Socket socket = null;
private String host;
private int port;
private int enemyX;
private int enemyY;
private int enemyRotate;
private int enemyBulletX;
private int enemyBulletY;
private boolean enemyHasLaser;
private boolean enemyHasShield;
private boolean enemyHasMoreLife;
private int myX, myY, myRotate;
private ArrayList<BulletState> myBullets;
private WallState wallState;
private ArrayList<EnemyTank> enemyTanks;
private ArrayList<BulletState> eachTankBullets;
private String serverAns;
private String[] enemyComponents;
private int numOfEnemies;
private String wallMap;
public Client1(String address , int port) {
this.host = address;
this.port = port;
numOfEnemies = 0;
}
private String tankStateToString (TankState tank) throws IOException {
String tankComponent , bulletComponent = "";
tankComponent = tank.locX +" "+ tank.locY+" " +tank.rotate +" "+tank.hasShield() +" "+tank.hasLaser() +" "+tank.isHasMoreLife();
for (BulletState bullet : tank.getBullets()){
bulletComponent +=" " + bullet.locX + " " + bullet.locY + " " ;
}
return tankComponent + bulletComponent;
}
public void run(TankState tankState) {
try{
......@@ -50,15 +36,13 @@ public class Client1 {
InputStream in = socket.getInputStream();
out.write("Client1".getBytes());
String text = "";
String clientText = "";
boolean running = true;
do {
//ToDo update the tank and enemy tank x , y , ...
text = tankStateToString(tankState);
running = !text.equalsIgnoreCase("over");
out.write(text.getBytes());
clientText = myTankComponents();
running = !clientText.equalsIgnoreCase("over");
out.write(clientText.getBytes());
byte[] buffer = new byte[2048];
int read = in.read(buffer);
......@@ -74,44 +58,55 @@ public class Client1 {
}
}
private ArrayList<EnemyTank> createEnemyTanks(String s){
private int getNumOfEnemies(){
String[] s = serverAns.split(" ");
numOfEnemies = s.length/6;
return numOfEnemies;
}
public ArrayList<EnemyTank> createEnemyTanks(){
ArrayList<EnemyTank> enemyTanks = new ArrayList<>();
int numOfEnemies = findNumOfEnemies(s);
for (int i = 0 ; i < numOfEnemies ; i ++){
for (int i = 0 ; i < getNumOfEnemies() ; i ++){
EnemyTank enemyTank = new EnemyTank(wallState);
enemyTanks.add(enemyTank);
}
return enemyTanks;
}
//ToDo
private int findNumOfEnemies(String s){
public void updateEnemyComponents(ArrayList<EnemyTank> enemyTanks){//ToDO separate tanks and each tank bullets
int j = 0;
enemyComponents = serverAns.split(" ");
wallMap = enemyComponents[0];
for (int i = 1 ; i < getNumOfEnemies() ; i++){
enemyTanks.get(i).locX = Integer.parseInt(enemyComponents[j]);j++;
enemyTanks.get(i).locY = Integer.parseInt(enemyComponents[j]);j++;
enemyTanks.get(i).rotate = Integer.parseInt(enemyComponents[j]);j++;
for (BulletState bullet : enemyTanks.get(i).getBullets()){
bullet.locX = Integer.parseInt(enemyComponents[j]);j++;
bullet.locY = Integer.parseInt(enemyComponents[j]);j++;
}
}
}
private void parser(){//ToDO separate tanks and each tank bullets
public void updateMyTank (TankState myTank) throws IOException {
myX = myTank.locX;
myY = myTank.locY;
myRotate = myTank.rotate;
myBullets = myTank.getBullets();
}
public void updateEnemyComponents(){
for (EnemyTank enemyTank : enemyTanks){
enemyTank.locX = enemyX;
enemyTank.locY = enemyY;
enemyTank.rotate =enemyRotate;
enemyTank.setHasShield(enemyHasShield);
enemyTank.setHasLaser(enemyHasLaser);
enemyTank.setHasMoreLife(enemyHasMoreLife);
for (BulletState bullet : enemyTank.getBullets()){
bullet.locX = enemyBulletX;
bullet.locY = enemyBulletY;
}
}
private String myTankComponents(){
String tankComponent , bulletComponent = "";
tankComponent = myX +" "+ myY +" " + myRotate ;
for (BulletState bullet : myBullets){
bulletComponent +=" " + bullet.locX + " " + bullet.locY ;
}
public ArrayList<EnemyTank> getEnemyTanks() {
return enemyTanks = createEnemyTanks(serverAns);
return tankComponent+bulletComponent;
}
public WallState getWallState() {
wallState.setSource(wallMap);
return wallState;
}
}
......@@ -137,6 +137,10 @@ public class WallState {
return false;
}
public void setSource(String source) {
this.source = source;
}
public ArrayList<WallData> getWalls() {
return walls;
}
......
......@@ -5,6 +5,7 @@ import UI.GameState.EnemyTank;
import UI.GameState.MyTank;
import UI.LogIn.Game.GameFrame;
import java.io.IOException;
import java.util.ArrayList;
/*** In The Name of Allah ***/
......@@ -48,7 +49,7 @@ import java.util.ArrayList;
// Perform all initializations ...
myTank = new MyTank(canvas.wallState);
client1.run(myTank);
enemyTanks = client1.getEnemyTanks();
enemyTanks = client1.createEnemyTanks();
canvas.addKeyListener(myTank.getMyListener());
canvas.addMouseListener(myTank.getMouseListener());
canvas.addMouseMotionListener(myTank.getMouseMotionListener());
......@@ -62,12 +63,13 @@ import java.util.ArrayList;
try {
long start = System.currentTimeMillis();
myTank.update();
client1.updateEnemyComponents();
client1.updateEnemyComponents(enemyTanks);
client1.updateMyTank(myTank);
canvas.render(myTank , enemyTanks , client1.getWallState());
long delay = (1000 / FPS) - (System.currentTimeMillis() - start);
if (delay > 0)
Thread.sleep(delay);
} catch (InterruptedException ex) {
} catch (InterruptedException | IOException ex) {
}
}
}
......
......@@ -152,7 +152,7 @@ public class NWGRendering {
if ((bullet.locX > myTank.locX - 20 && bullet.locX < myTank.locX + 20 && bullet.locY > myTank.locY - 10 && bullet.locY < myTank.locY + 10) && !myTank.hasShield() && System.currentTimeMillis() - bullet.getCreateTime() > 100 && !myTank.isHasMoreLife()) {
startExploding = System.currentTimeMillis();
if (System.currentTimeMillis() - startExploding < 1000) {
g2d.drawImage(op2.filter((BufferedImage) explosionImg, null), myTank.locX, myTank.locY, null);
g2d.drawImage(op.filter((BufferedImage) explosionImg, null), myTank.locX, myTank.locY, null);//????????
myTank.setTankLife(0);
myTank.gameOver = true;
}
......@@ -172,6 +172,12 @@ public class NWGRendering {
if (enemyTank.getTankLife() <= 0) {
startExploding = System.currentTimeMillis();
if (System.currentTimeMillis() - startExploding < 1000) {
double rotationEnemy = Math.toRadians(enemyTank.rotate);
BufferedImage image22 = (BufferedImage) image2;
double locationX2 = image22.getWidth() / 2;
double locationY2 = image22.getHeight() / 2;
AffineTransform tx2 = AffineTransform.getRotateInstance(rotationEnemy, locationX2, locationY2);
AffineTransformOp op2 = new AffineTransformOp(tx2, AffineTransformOp.TYPE_BILINEAR);
g2d.drawImage(op2.filter((BufferedImage) explosionImg, null), enemyTank.locX, enemyTank.locY, null);
enemyTank.setTankLife(0);
enemyTank.gameOver = true;
......@@ -186,32 +192,6 @@ public class NWGRendering {
}
}
//Draw rewards
boolean newReward = (System.currentTimeMillis() - GameLoop.startTime >= 7000);
if (newReward) {
GameLoop.startTime = System.currentTimeMillis();
RewardState reward = new RewardState(myTank.getWalls());
reward.setVisible(true);
rewards.add(reward);
}
if (rewards.size() != 0) {
Iterator<RewardState> it = rewards.iterator();
while (it.hasNext()) {
RewardState reward = it.next();
if (reward.isVisible() && reward.isRewardForTank(myTank.locX, myTank.locY)) {
reward.runReward(myTank);
reward.setVisible(false);
it.remove();
} else if (reward.isVisible() && reward.isRewardForTank(enemyTank.locX, enemyTank.locY)) {
reward.runReward(enemyTank);
reward.setVisible(false);
it.remove();
} else if (reward.isVisible()) {
g2d.drawImage(reward.getRewardImage(), reward.locX, reward.locY, null);
}
}
}
// Print FPS info
......
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