summaryrefslogtreecommitdiff
path: root/shared/src/main/java/kz/ilotterytea/frogartha/domain/PlayerState.java
blob: c3db5a889eaee6816105b652807b1a0eb95c981c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
package kz.ilotterytea.frogartha.domain;

import com.badlogic.gdx.math.Vector2;
import com.badlogic.gdx.math.Vector3;
import com.github.czyzby.websocket.serialization.SerializationException;
import com.github.czyzby.websocket.serialization.Transferable;
import com.github.czyzby.websocket.serialization.impl.Deserializer;
import com.github.czyzby.websocket.serialization.impl.Serializer;

public class PlayerState implements Transferable<PlayerState> {
    private final Vector3 position;
    private final Vector2 direction;
    private float nextJumpTimestamp;
    private long lastMessageTimestamp;

    public PlayerState() {
        this(new Vector3(), new Vector2());
    }

    public PlayerState(Vector3 position, Vector2 direction) {
        this.position = position;
        this.direction = direction;
        this.nextJumpTimestamp = 0;
    }

    public Vector3 getPosition() {
        return position;
    }

    public void setPosition(float x, float y, float z) {
        this.position.set(x, y, z);
    }

    public Vector2 getDirection() {
        return direction;
    }

    public float getNextJumpTimestamp() {
        return nextJumpTimestamp;
    }

    public void setNextJumpTimestamp(float nextJumpTimestamp) {
        this.nextJumpTimestamp = nextJumpTimestamp;
    }

    public long getLastMessageTimestamp() {
        return lastMessageTimestamp;
    }

    public void setLastMessageTimestamp(long lastMessageTimestamp) {
        this.lastMessageTimestamp = lastMessageTimestamp;
    }

    public void setDirection(float x, float z) {
        this.direction.set(x, z);
    }

    @Override
    public void serialize(Serializer serializer) throws SerializationException {
        serializer
            .serializeFloatArray(new float[]{position.x, position.y, position.z})
            .serializeFloat(direction.x)
            .serializeFloat(direction.y);
    }

    @Override
    public PlayerState deserialize(Deserializer deserializer) throws SerializationException {
        return new PlayerState(
            new Vector3(deserializer.deserializeFloatArray()),
            new Vector2(deserializer.deserializeFloat(), deserializer.deserializeFloat())
        );
    }
}