Getting and Sending Data between a Server and Client

94 Views Asked by At

We are making a six-player multiplayer game and I am responsible for a networking class. The game incorporates Slick 2D. In the game there are entities(code shown below)

    package Game; 

import org.newdawn.slick.*;

public abstract class Entity {
    double maxHealth, health, speed, damage, width, height, locationX, locationY;
    Animation standing, walkingLeft, walkingRight, attacking, jumping, current;
    long pastTime = 0;

    public boolean isReadyToAttack(long delta) {
        if(pastTime < 2 * 500) { //multiply by 1000 to get milliseconds
            pastTime += delta;
            return false;
        }else{
            pastTime = 0;
            return true;
        }
    }

    public void setMaxHealth(double d){
        maxHealth = d;
    }
    public void setHealth(double d){
        health = d;
    }

    public void setSpeed(double x){
        speed = x;
    }

    public void setDamage(double x){
        damage = x;
    }
    public void setLocation(double x, double y){
        locationX = x;
        locationY = y;
    }
    public void setSprites(Animation s, Animation r){
        standing = s;
        walkingRight = r;
    }
    public void setCurrent(Animation a){
        current = a;
    }
    public double getMaxHealth(){
        return maxHealth;
    }
    public double getHealth(){
        return health;
    }

    public double getSpeed(){
        return speed;
    }

    public double getDamage(){
        return damage;
    }

    public double getLocationX(){
        return locationX;
    }

    public double getLocationY(){
        return locationY;
    }
    public Animation getStanding(){
        return standing;
    }
    public Animation getRight(){
        return walkingRight;
    }
    public Animation getCurrent(){
        return current;
    }
    public abstract double getAttackRange();
}

The entities class is also extended by the Heroes class which is further extended by the Ares class. Ares is one of the six characters in our game and when you start the game you can choose one of the six in the start menu.

Here is the Hero class:

    package Game;

import org.newdawn.slick.Animation;

public abstract class Hero extends Entity{

    static double gravity = 0.1;
    double velocity;
    int level;
    boolean disabled, grounded = true;

    public void setLevel(int i){
        level = i;
    }
    public void setVelocity(double d){
        velocity = d;
    }
    public void setDisabled(boolean b){
        disabled = b;
    }
    public void setGrounded(boolean b){
        grounded = b;
    }
    public int getLevel(){
        return level;
    }
    public double getVelocity(){
        return velocity;
    }
    public boolean getDisabled(){
        return disabled;
    }
    public boolean getGrounded(){
        return grounded;
    }
    public double getGravity(){
        return gravity;
    }

    public boolean isAlive(){
        if(getHealth() > 0)
            return true;
        return false;
    }

    public void attack(Entity gettingAttacked, Hero attacking) {
        gettingAttacked.setHealth(gettingAttacked.getHealth() - attacking.getDamage());
    }

    public boolean isReadyToRegenerate(long delta) {
        if(pastTime < 1 * 1000){
            pastTime += delta;
            return false;
        }
        else{
            pastTime = 0;
            return true;
        }
    }   

    public static void regenerate(Hero h, long delta){
        if(h.getHealth() >= 0 && h.getHealth() < h.getMaxHealth())
            if(h.isReadyToRegenerate(delta))
                h.setHealth(h.getHealth() + (h.getMaxHealth()) * 0.01);
    }
    public abstract void levelUp();
}

Here is the Ares class:

   package Game;

import org.newdawn.slick.Animation;
import org.newdawn.slick.SlickException;
import org.newdawn.slick.SpriteSheet;

public class Ares extends Hero {

    public Ares() throws SlickException {
        setHealth(500);
        setSpeed(0.4);
        setDamage(70);
        setLocation(850,625);
        setSprites(new Animation(new SpriteSheet("res/Ares.png", 191, 275), 200), new Animation(new SpriteSheet("res/AresWalk.png", 191, 275), 200));
    }

    public void levelUp() {
        level += 1;
        health *= 1.1;
        damage *= 1.1;
    }

    public double getAttackRange() {
        return 250;
    }

    public double maxHealth() {
        return 500;
    }

}

Here is the Battle code, which contains the main gameplay code, the code is currently uncompleted and only has movement:

 package Game;

import java.util.ArrayList;

import org.lwjgl.input.*;
import org.newdawn.slick.*;
import org.newdawn.slick.geom.Rectangle;
import org.newdawn.slick.state.*;

public class Battle extends BasicGameState {

    private ArrayList<Entity> entities = new ArrayList<Entity>();
    private Hero player;
    private Image background;
    private int midScreen, ground;

    public void init(GameContainer gc, StateBasedGame sbg) throws SlickException{
        gc.setVSync(true);
        player = new Ares();
        player.setCurrent(player.getStanding());
        entities.add(player);
        background = new Image("res/Background.png");
        midScreen = 800;
        ground = 625;
    }


    public void render(GameContainer gc, StateBasedGame sbg, Graphics g) throws SlickException{
        g.translate((float)-player.getLocationX() + midScreen, (float)-player.getLocationY() + ground);
        background.draw(0, -920);
        for(int i = 0; i < entities.size(); i++)
            entities.get(i).getCurrent().draw((float)entities.get(i).getLocationX(), (float)entities.get(i).getLocationY());
    }

    public void update(GameContainer gc, StateBasedGame sbg, int delta) throws SlickException {
            player.getCurrent().update(delta); // ensures
            Input kb = gc.getInput();

            if (kb.isKeyDown(Input.KEY_SPACE) && player.getGrounded()) {
                player.setGrounded(false);
                player.setVelocity(7);
               player.setLocation(player.getLocationX(), player.getLocationY() - player.getVelocity());
                player.setCurrent(player.getStanding());
            }
            else if (kb.isKeyDown(Input.KEY_A) && player.getLocationX() > 800) {
                player.setLocation(player.getLocationX() - player.getSpeed() * delta, player.getLocationY());
                if(player.grounded)
                    player.setCurrent(player.getRight());
            }
            else if (kb.isKeyDown(Input.KEY_D) && player.getLocationX() < 8580) {
                player.setLocation(player.getLocationX() + player.getSpeed() * delta, player.getLocationY());
                if(player.grounded)
                    player.setCurrent(player.getRight());
            }
            else
                player.setCurrent(player.getStanding());

            if (Math.abs(player.getLocationY() - ground) < .01)
                player.setGrounded(true);
            if (player.getGrounded() == false) {
                player.setVelocity(player.getVelocity() - player.getGravity());
                player.setLocation(player.getLocationX(), player.getLocationY() - player.getVelocity());
            }  
    }    

    public int getID(){
        return 1;
    }
}

What I am having difficulty with is creating a server and client that gets the input from the players, such as location, who chose which player, etc. I'm wondering that if in Battle, when Player objects are made, how would I be able to get location and other specific attributes of the objects and give them to the other computers.

Note: I am coding in Java, and trying to use a TCP connection to code this. I am open to UDP if you think it may be easier. Just give a basic overview of what needs to be done and perhaps a little code, do not simply give me the answer please.

0

There are 0 best solutions below