package electrosphere.net.server.player;

import electrosphere.engine.Globals;
import electrosphere.entity.Entity;
import electrosphere.net.parser.net.message.NetworkMessage;
import electrosphere.net.server.ServerConnectionHandler;

import java.util.concurrent.Semaphore;

import org.joml.Vector3i;

/**
 * A client logged into the server
 */
public class Player {
    
    ServerConnectionHandler connectionHandler;
    int id;
    static Semaphore idIncrementerLock = new Semaphore(1);
    static int idIncrementer = 0;
    Vector3i worldPos;
    int simulationRadius = 3;
    Entity playerEntity;
    
    public Player(ServerConnectionHandler connectionHandler){
        this.connectionHandler = connectionHandler;
        idIncrementerLock.acquireUninterruptibly();
        id = idIncrementer;
        idIncrementer++;
        idIncrementerLock.release();
        connectionHandler.setPlayerId(id);
        this.simulationRadius = Globals.userSettings.getGameplayPhysicsCellRadius();
    }

    //used for when client is creating a player object for itself
    public Player(int id){
        this.id = id;
    }

    public int getId() {
        return id;
    }
    
    public void addMessage(NetworkMessage message){
        connectionHandler.addMessagetoOutgoingQueue(message);
    }

    public Vector3i getWorldPos() {
        return worldPos;
    }

    public void setWorldPos(Vector3i worldPos) {
        this.worldPos = worldPos;
    }

    public int getSimulationRadius() {
        return simulationRadius;
    }

    public void setSimulationRadius(int simulationRadius) {
        this.simulationRadius = simulationRadius;
    }

    public Entity getPlayerEntity() {
        return playerEntity;
    }

    public void setPlayerEntity(Entity playerEntity) {
        this.playerEntity = playerEntity;
    }
    
    
}
