Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feature/server-side-and-database #29

Open
wants to merge 14 commits into
base: feature/server-side-and-database
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
.idea/*

ServerSide/.idea/*

ServerSide/*.iml
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@ public ComponentManagerImpl() {
components.put(ComponentType.MASS_COMPONENT, new Mass());
components.put(ComponentType.HEALTH_COMPONENT, new Health());
components.put(ComponentType.LOCATION_COMPONENT, new Location());
components.put(ComponentType.DIRECTION_COMPONENT, new Direction());
components.put(ComponentType.SPEED_COMPONENT, new Speed());
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,17 @@

package ru.servers.gameserver.ecs.components;

import lombok.AllArgsConstructor;
import lombok.Getter;

@AllArgsConstructor
@Getter
public enum ComponentType {
HEALTH_COMPONENT,
LOCATION_COMPONENT,
MASS_COMPONENT,
DIRECTION_COMPONENT,
SPEED_COMPONENT

HEALTH_COMPONENT(Health.class),
LOCATION_COMPONENT(Location.class),
MASS_COMPONENT(Mass.class),
SPEED_COMPONENT(Speed.class);

private Class componentClass;
}

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -16,22 +16,36 @@

package ru.servers.gameserver.ecs.entities;

import lombok.AllArgsConstructor;
import lombok.EqualsAndHashCode;
import lombok.Getter;
import lombok.ToString;
import ru.servers.gameserver.ecs.components.Component;
import ru.servers.gameserver.ecs.components.ComponentType;

import java.util.Iterator;
import java.util.Map;

public interface Entity {
@AllArgsConstructor
@Getter
@ToString
@EqualsAndHashCode
public abstract class Entity {

void addComponent(ComponentType componentType, Component component);
private Map<ComponentType, Component> components;

Component getComponent(ComponentType componentType);
protected boolean componentTypeIsCorrect(ComponentType componentType, Component component) {
return componentType.getComponentClass().equals(component.getClass());
}

Iterator<Map.Entry<ComponentType, Component>> getComponents();
public abstract void addComponent(ComponentType componentType, Component component);

int getCountComponents();
public abstract Component getComponent(ComponentType componentType);

void removeComponent(ComponentType componentType);
public abstract Iterator<Map.Entry<ComponentType, Component>> getIteratorComponents();

public abstract int getCountComponents();

public abstract void removeComponent(ComponentType componentType);

}
Original file line number Diff line number Diff line change
Expand Up @@ -16,46 +16,44 @@

package ru.servers.gameserver.ecs.entities;

import lombok.EqualsAndHashCode;
import lombok.NoArgsConstructor;
import lombok.ToString;
import ru.servers.gameserver.ecs.components.Component;
import ru.servers.gameserver.ecs.components.ComponentType;

import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;

@ToString
@EqualsAndHashCode
@NoArgsConstructor
public class Vehicle implements Entity{
public class Vehicle extends Entity {

private Map<ComponentType, Component> components = new HashMap<>();
public Vehicle() {
super(new HashMap<>());
}

@Override
public void addComponent(ComponentType componentType, Component component) {
components.put(componentType, component);
if (componentTypeIsCorrect(componentType, component)) {
getComponents().put(componentType, component);
}
}

@Override
public Component getComponent(ComponentType componentType) {
return components.get(componentType);
return getComponents().get(componentType);
}

@Override
public Iterator<Map.Entry<ComponentType, Component>> getComponents() {
return components.entrySet().iterator();
public Iterator<Map.Entry<ComponentType, Component>> getIteratorComponents() {
return getComponents().entrySet().iterator();
}

@Override
public int getCountComponents() {
return components.size();
return getComponents().size();
}

@Override
public void removeComponent(ComponentType componentType) {
components.remove(componentType);
getComponents().remove(componentType);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,77 @@

package ru.servers.gameserver.ecs.systems;

public class MoveSystem { }
import ru.servers.gameserver.ecs.components.Component;
import ru.servers.gameserver.ecs.components.ComponentType;
import ru.servers.gameserver.ecs.components.Location;
import ru.servers.gameserver.ecs.components.Speed;
import ru.servers.gameserver.ecs.entities.Entity;
import ru.servers.gameserver.math.algebra.Quaternion;
import ru.servers.gameserver.math.algebra.vectors.Vector3;
import ru.servers.gameserver.math.algebra.vectors.VectorsUtil;

import java.util.Set;
import java.util.stream.Collectors;

public class MoveSystem implements System {

@Override
public void execute(long milliseconds) {
getFilteredEntities()
.forEach(entity -> {
Speed speed = (Speed) entity.getComponent(ComponentType.SPEED_COMPONENT);
Location location = (Location) entity.getComponent(ComponentType.LOCATION_COMPONENT);
double oldSpeed = speed.getSpeed();
speed.setSpeed(speed.getSpeed() + speed.getAcceleration() * milliseconds);
Vector3 deltaPositionVector3 = new Vector3(
getDistanceByAxis(VectorsUtil.getAngleBetweenVectors(location.getDirection(),
new Vector3(location.getDirection().getX(), 0, 0)), speed, oldSpeed),
getDistanceByAxis(VectorsUtil.getAngleBetweenVectors(location.getDirection(),
new Vector3(0, location.getDirection().getY(), 0)), speed, oldSpeed),
getDistanceByAxis(VectorsUtil.getAngleBetweenVectors(location.getDirection(),
new Vector3(0, 0, location.getDirection().getZ())), speed, oldSpeed)
);
Vector3 deltaDirectionVector3 = new Vector3(
location.getPosition().getX(),
location.getPosition().getY(),
location.getPosition().getZ()
);
deltaDirectionVector3.sub(location.getDirection());
deltaPositionVector3 = rotateVector(deltaPositionVector3,
VectorsUtil.getAngleBetweenVectors(deltaPositionVector3, deltaDirectionVector3));
deltaDirectionVector3.add(deltaPositionVector3);
location.getDirection().add(deltaDirectionVector3);
});
}

@Override
public Set<Entity> getFilteredEntities() {
return System.entities.stream()
.filter(entity -> {
Component component = entity.getComponent(ComponentType.SPEED_COMPONENT);
if (component != null) {
return ((Speed) component).getSpeed() != 0;
}
return false;
})
.collect(Collectors.toSet());
}

private double getDistanceByAxis(double angleWithSpecifyVector, Speed speed, double oldSpeed) {
return (Math.pow(Math.cos(angleWithSpecifyVector) * speed.getSpeed(), 2)
- Math.pow(Math.cos(angleWithSpecifyVector) * oldSpeed, 2))
/ (2 * speed.getAcceleration());
}

private Vector3 rotateVector(Vector3 vector3, double angle) {
Quaternion quaternion = new Quaternion(Math.cos(angle / 2), vector3);
Vector3 reversedVector3 = new Vector3(vector3.getX(), vector3.getY(), vector3.getZ());
reversedVector3.mul(-1);
Quaternion conjugatedQuaternion = new Quaternion(quaternion.getW(), reversedVector3);
double normal = Math.pow(vector3.getX(), 2) + Math.pow(vector3.getY(), 2) + Math.pow(vector3.getZ(), 2);
conjugatedQuaternion.mul(1 / normal);
quaternion.mul(new Quaternion(0, vector3));
quaternion.mul(conjugatedQuaternion);
return quaternion.getVector();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,16 @@

package ru.servers.gameserver.ecs.systems;

public interface System { }
import ru.servers.gameserver.ecs.entities.Entity;

import java.util.HashSet;
import java.util.Set;

public interface System {

Set<Entity> entities = new HashSet<>();

void execute(long milliseconds);

Set<Entity> getFilteredEntities();
}