通过套接字发送时删除定时的问题

时间:2016-06-19 03:19:19

标签: java sockets object time

我有一个游戏设置,我在服务器和客户端之间的套接字上发送一系列Tower和Troop对象,由于某种原因,攻击率和动画(这让我相信时间属性)被忽略/编辑。这是塔类:

package jandek.towers;

import java.awt.Color;
import java.awt.Graphics;
import java.awt.image.BufferedImage;
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.Serializable;
import java.util.ArrayList;

import javax.imageio.ImageIO;

import jandek.handler.Handler;
import jandek.main.Frame;
import jandek.main.Game;
import jandek.utils.Utils;


public abstract class Tower implements Serializable{

private int range;
private double attackDMG;
private double attackSPEED; //1 = 1/60 seconds
private byte[][][] image;
private int x;
private int y;
private long lastAttackTime;
private int multiplier;
private int level; //0 = 1;
private boolean moving;
private boolean rangeNeedsToBeDrawn;
private int width = 32;
private int height = 32;
//private BufferedImage[][][] bufferedImages = new BufferedImage[16][4][2];
private ArrayList<Integer> troopsInTroopsArrayThatAreInRange;
private int animationFrame = 0;
private int animationFrameSpeed = 60; //nano seconds between changes
private double animationFrameLastChange; //last change
boolean attacked;
boolean animation = false;
/**
 * 
 * @param x
 * @param y
 * @param level
 * @param multiplier
 * @param range
 * @param attackDMG
 * @param attackSPEED
 * @param image
 */
public Tower(int x, int y, int level, int multiplier, int range, double attackDMG, double attackSPEED, byte[][][] image){
    troopsInTroopsArrayThatAreInRange = new ArrayList<Integer>();
    this.x = x;
    this.y = y;
    this.range = range;
    this.attackDMG = attackDMG + (level * multiplier);
    this.attackSPEED = attackSPEED;
    this.image = image;
    this.level = level;
    this.multiplier = multiplier;
    lastAttackTime = Handler.getGame().getTime();
    moving = false;
    rangeNeedsToBeDrawn = true;

}
public void update(){
    attacked = false;
    if(moving == true){
        setX(Frame.getMouseX());
        setY(Frame.getMouseY());
    }
    AttackTarget();
    if(attacked){
        animation = true;
        animationFrameLastChange = Handler.getGame().getTime();
        animationFrame = 1;
        //Handler.getGame().getLogWriter().write("Animation is at the second image");
    }

    if(animation && Handler.getGame().getTime() - animationFrameLastChange >= animationFrameSpeed){
        animationFrame = 0;
        animation = false;
        }
}

private void AttackTarget(){
    if((Handler.getGame().getTime() - lastAttackTime) > attackSPEED && !moving){
        for(int i = 0; i < Handler.getTroops().size() ; i++){
            int x1 = this.x + this.width / 2;
            int y1 = this.y + this.height / 2;
            int x2 = Handler.getTroops().get(i).getX() + Handler.getTroops().get(i).getSize() / 2;
            int y2 = Handler.getTroops().get(i).getY() + Handler.getTroops().get(i).getSize() / 2;
            if(Utils.getDistance(x1, y1, x2, y2) <= range){
                troopsInTroopsArrayThatAreInRange.add(i);
                attacked = true;
            }
        }
    }

    if(attacked){
        Handler.getGame().getLogWriter().write("");
        Handler.getGame().getLogWriter().write(Handler.getGame().getTime() - lastAttackTime + " : " + attackSPEED);
        lastAttackTime = Handler.getGame().getTime();
        Handler.getGame().getLogWriter().write(Handler.getGame().getTime() - lastAttackTime + " : " + attackSPEED);
        Handler.getGame().getLogWriter().write("");
        double min = range;
        int nearestTroop = -1;
        for(int j = 0; j < troopsInTroopsArrayThatAreInRange.size(); j++){
            int x1 = this.x + this.width / 2;
            int y1 = this.y + this.height / 2;
            int x2 = Handler.getTroops().get(troopsInTroopsArrayThatAreInRange.get(j)).getX() + Handler.getTroops().get(troopsInTroopsArrayThatAreInRange.get(j)).getSize() / 2;
            int y2 = Handler.getTroops().get(troopsInTroopsArrayThatAreInRange.get(j)).getY() + Handler.getTroops().get(troopsInTroopsArrayThatAreInRange.get(j)).getSize() / 2;
            if(Utils.getDistance(x1, y1, x2, y2) < min){
                min = Utils.getDistance(x1, y1, x2, y2);
            //  Handler.getGame().getLogWriter().write("troop distance checked");   
                nearestTroop = troopsInTroopsArrayThatAreInRange.get(j);
            }
        }
        Handler.getTroops().get(nearestTroop).changeHealth(-(int)attackDMG);
        troopsInTroopsArrayThatAreInRange.clear();
    }


}

}

在运行方法中调用update方法,该方法每秒运行60次,并且Handler.getGame()。getTime()在同一运行方法中递增。这是来自Handler.getGame()的日志.getLogWriter():

505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520

206 : 120.0
0 : 120.0

521

207 : 120.0
0 : 120.0

522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556

在运行方法结束时打印出上面的每个“1”,从而显示

Handler.getGame().getTime() - lastAttackTime

以某种方式通过套接字被打扰。有任何想法吗?我希望我不会错过一些过于明显或简单的东西。

编辑套接字代码:

private void updateConnection(){
        if(isClientActive()){
            if(player.getType() == 0){
                client.sendObjects(towers);
            }else{
                client.sendObjects(troops);
            }
            ArrayList<Object> obj = (ArrayList<Object>)client.receiveObjects();
            if(obj != null && obj.size() > 0){
                if (obj.get(0) instanceof Troop) {
                    mendTroops(obj);
                }else if(obj.get(0) instanceof Troop){
                    mendTowers(obj);
                }
            }else{
                if(player.getType() == 0){
                    troops.clear();
                }else{
                    towers.clear();
                }
            }
        }else if(isServerActive()){
            if(player.getType() == 0){
                server.sendObjects(towers);
            }else{
                server.sendObjects(troops);
            }
            ArrayList<Object> obj = (ArrayList<Object>)server.receiveObjects();
            if(obj != null && obj.size() > 0){
                if (obj.get(0) instanceof Troop) {
                    mendTroops(obj);
                }else if(obj.get(0) instanceof Tower){
                    mendTowers(obj);
                }
            }else{
                if(player.getType() == 0){
                    troops.clear();
                }else{
                    towers.clear();
                }
            }
        }
    } 

1 个答案:

答案 0 :(得分:0)

我的帖子中没有看到任何套接字代码,但TCP和UDP都没有提供任何时序保证。 TCP广泛使用缓冲来将输出写入合并到较少数量的线段中。你可以通过禁用Nagle算法来缓解这种情况,但是你永远无法从任何网络中获得实时性能。