Browse Source

basic interfaces

master
Maggistrator 1 year ago
commit
9e4de7a771
  1. 2
      .gitignore
  2. 73
      build.xml
  3. BIN
      data/images/test/ecs/eyes.PNG
  4. BIN
      lib/slick/docs/slick-javadoc.zip
  5. BIN
      lib/slick/jars/ibxm.jar
  6. BIN
      lib/slick/jars/jinput.jar
  7. BIN
      lib/slick/jars/jna-4.3.0.jar
  8. BIN
      lib/slick/jars/jnlp.jar
  9. BIN
      lib/slick/jars/jogg-0.0.7.jar
  10. BIN
      lib/slick/jars/jorbis-0.0.15.jar
  11. BIN
      lib/slick/jars/lwjgl.jar
  12. BIN
      lib/slick/jars/lwjgl_util.jar
  13. BIN
      lib/slick/jars/natives-linux.jar
  14. BIN
      lib/slick/jars/natives-mac.jar
  15. BIN
      lib/slick/jars/natives-windows.jar
  16. BIN
      lib/slick/jars/slick.jar
  17. BIN
      lib/slick/natives/OpenAL32.dll
  18. BIN
      lib/slick/natives/OpenAL64.dll
  19. BIN
      lib/slick/natives/jinput-dx8.dll
  20. BIN
      lib/slick/natives/jinput-dx8_64.dll
  21. BIN
      lib/slick/natives/jinput-raw.dll
  22. BIN
      lib/slick/natives/jinput-raw_64.dll
  23. BIN
      lib/slick/natives/libjinput-linux.so
  24. BIN
      lib/slick/natives/libjinput-linux64.so
  25. BIN
      lib/slick/natives/libjinput-osx.dylib
  26. BIN
      lib/slick/natives/liblwjgl.dylib
  27. BIN
      lib/slick/natives/liblwjgl.so
  28. BIN
      lib/slick/natives/liblwjgl64.so
  29. BIN
      lib/slick/natives/libopenal.so
  30. BIN
      lib/slick/natives/libopenal64.so
  31. BIN
      lib/slick/natives/lwjgl.dll
  32. BIN
      lib/slick/natives/lwjgl64.dll
  33. BIN
      lib/slick/natives/openal.dylib
  34. BIN
      lib/slick/src/slick-src.zip
  35. 3
      manifest.mf
  36. 12
      src/engine/ecs/Aspect.java
  37. 16
      src/engine/ecs/Component.java
  38. 17
      src/engine/ecs/Entity.java
  39. 137
      src/engine/ecs/EntityManager.java
  40. 13
      src/engine/ecs/RenderableSystem.java
  41. 12
      src/engine/ecs/System.java
  42. 39
      src/game/GameWorld.java
  43. 27
      src/game/components/PositionComponent.java
  44. 20
      src/game/components/SpriteRendererComponent.java
  45. 44
      src/game/systems/SpriteRendererSystem.java
  46. 27
      src/test/ecs/ECSTestLauncher.java
  47. 54
      src/test/ecs/ECSWorld.java

2
.gitignore

@ -0,0 +1,2 @@
/build
/nbproject

73
build.xml

@ -0,0 +1,73 @@
<?xml version="1.0" encoding="UTF-8"?>
<!-- You may freely edit this file. See commented blocks below for -->
<!-- some examples of how to customize the build. -->
<!-- (If you delete it and reopen the project it will be recreated.) -->
<!-- By default, only the Clean and Build commands use this build script. -->
<!-- Commands such as Run, Debug, and Test only use this build script if -->
<!-- the Compile on Save feature is turned off for the project. -->
<!-- You can turn off the Compile on Save (or Deploy on Save) setting -->
<!-- in the project's Project Properties dialog box.-->
<project name="CelestiaSunriseECS" default="default" basedir=".">
<description>Builds, tests, and runs the project CelestiaSunriseECS.</description>
<import file="nbproject/build-impl.xml"/>
<!--
There exist several targets which are by default empty and which can be
used for execution of your tasks. These targets are usually executed
before and after some main targets. They are:
-pre-init: called before initialization of project properties
-post-init: called after initialization of project properties
-pre-compile: called before javac compilation
-post-compile: called after javac compilation
-pre-compile-single: called before javac compilation of single file
-post-compile-single: called after javac compilation of single file
-pre-compile-test: called before javac compilation of JUnit tests
-post-compile-test: called after javac compilation of JUnit tests
-pre-compile-test-single: called before javac compilation of single JUnit test
-post-compile-test-single: called after javac compilation of single JUunit test
-pre-jar: called before JAR building
-post-jar: called after JAR building
-post-clean: called after cleaning build products
(Targets beginning with '-' are not intended to be called on their own.)
Example of inserting an obfuscator after compilation could look like this:
<target name="-post-compile">
<obfuscate>
<fileset dir="${build.classes.dir}"/>
</obfuscate>
</target>
For list of available properties check the imported
nbproject/build-impl.xml file.
Another way to customize the build is by overriding existing main targets.
The targets of interest are:
-init-macrodef-javac: defines macro for javac compilation
-init-macrodef-junit: defines macro for junit execution
-init-macrodef-debug: defines macro for class debugging
-init-macrodef-java: defines macro for class execution
-do-jar: JAR building
run: execution of project
-javadoc-build: Javadoc generation
test-report: JUnit report generation
An example of overriding the target for project execution could look like this:
<target name="run" depends="CelestiaSunriseECS-impl.jar">
<exec dir="bin" executable="launcher.exe">
<arg file="${dist.jar}"/>
</exec>
</target>
Notice that the overridden target depends on the jar target and not only on
the compile target as the regular run target does. Again, for a list of available
properties which you can use, check the target you are overriding in the
nbproject/build-impl.xml file.
-->
</project>

BIN
data/images/test/ecs/eyes.PNG

Binary file not shown.

After

Width:  |  Height:  |  Size: 37 KiB

BIN
lib/slick/docs/slick-javadoc.zip

Binary file not shown.

BIN
lib/slick/jars/ibxm.jar

Binary file not shown.

BIN
lib/slick/jars/jinput.jar

Binary file not shown.

BIN
lib/slick/jars/jna-4.3.0.jar

Binary file not shown.

BIN
lib/slick/jars/jnlp.jar

Binary file not shown.

BIN
lib/slick/jars/jogg-0.0.7.jar

Binary file not shown.

BIN
lib/slick/jars/jorbis-0.0.15.jar

Binary file not shown.

BIN
lib/slick/jars/lwjgl.jar

Binary file not shown.

BIN
lib/slick/jars/lwjgl_util.jar

Binary file not shown.

BIN
lib/slick/jars/natives-linux.jar

Binary file not shown.

BIN
lib/slick/jars/natives-mac.jar

Binary file not shown.

BIN
lib/slick/jars/natives-windows.jar

Binary file not shown.

BIN
lib/slick/jars/slick.jar

Binary file not shown.

BIN
lib/slick/natives/OpenAL32.dll

Binary file not shown.

BIN
lib/slick/natives/OpenAL64.dll

Binary file not shown.

BIN
lib/slick/natives/jinput-dx8.dll

Binary file not shown.

BIN
lib/slick/natives/jinput-dx8_64.dll

Binary file not shown.

BIN
lib/slick/natives/jinput-raw.dll

Binary file not shown.

BIN
lib/slick/natives/jinput-raw_64.dll

Binary file not shown.

BIN
lib/slick/natives/libjinput-linux.so

Binary file not shown.

BIN
lib/slick/natives/libjinput-linux64.so

Binary file not shown.

BIN
lib/slick/natives/libjinput-osx.dylib

Binary file not shown.

BIN
lib/slick/natives/liblwjgl.dylib

Binary file not shown.

BIN
lib/slick/natives/liblwjgl.so

Binary file not shown.

BIN
lib/slick/natives/liblwjgl64.so

Binary file not shown.

BIN
lib/slick/natives/libopenal.so

Binary file not shown.

BIN
lib/slick/natives/libopenal64.so

Binary file not shown.

BIN
lib/slick/natives/lwjgl.dll

Binary file not shown.

BIN
lib/slick/natives/lwjgl64.dll

Binary file not shown.

BIN
lib/slick/natives/openal.dylib

Binary file not shown.

BIN
lib/slick/src/slick-src.zip

Binary file not shown.

3
manifest.mf

@ -0,0 +1,3 @@
Manifest-Version: 1.0
X-COMMENT: Main-Class will be added automatically by build

12
src/engine/ecs/Aspect.java

@ -0,0 +1,12 @@
package engine.ecs;
/**
*
* @author Maggistrator
*/
public enum Aspect {
POSITION,
SPRITE
}

16
src/engine/ecs/Component.java

@ -0,0 +1,16 @@
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package engine.ecs;
/**
*
* @author Maggistrator
*/
public interface Component {
Aspect getAspect();
}

17
src/engine/ecs/Entity.java

@ -0,0 +1,17 @@
package engine.ecs;
/**
* Entity is an unique set of components, represented by it's id.
* Entity doesn't have it's own data or behavior - Components and Systems handles it.
* Entity doesn't contain any components or systems - just indicates them.
* @author Maggistrator
*/
public class Entity {
public int id;
public Entity(int id) {
this.id = id;
}
}

137
src/engine/ecs/EntityManager.java

@ -0,0 +1,137 @@
package engine.ecs;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Random;
import org.newdawn.slick.Graphics;
/**
* Main manager of all entities living in the world.
* This class is responcible for edding, deleting, and editing components of any entity living within it.
* Also stores and processing Systems, making modular game logic to execute.
*
* @author Maggistrator
*/
public class EntityManager {
private HashMap<Entity, LinkedList<Component>> components = new HashMap<>();
private ArrayList<System> systems = new ArrayList<>();
private ArrayList<Integer> usedIDs = new ArrayList<>();
public Entity createNewEntity(){
int id = nextAviableID();
Entity e = new Entity(id);
usedIDs.add(id);
components.put(e, new LinkedList<>());
return e;
}
public void addEntity(Entity e, Component... nc) {
List<Component> componentsListByEntity = this.components.get(e);
componentsListByEntity.addAll(Arrays.asList(nc));
}
public void addComponent(Entity e, Component c) {
List<Component> componentsListByEntity = this.components.get(e);
componentsListByEntity.add(c);
}
public <T extends Component> T getComponent( Entity e, Aspect a ){
List<Component> comps = components.get(e);
for(Component c: comps){
if(c.getAspect().equals(a)) return (T)c;
}
return null;
}
/**
* TODO: Íàïèñàòü ôóíêöèþ ïîëó÷åíèÿ ñóùíîñòè getEntity
* TODO: Ïîäóìàòü î òîì, ñòîèò ëè ïðèäåðæèâàòüñÿ ñòðîãîé ïàðàäèãìû
* ECS, è îòêàçûâàòüñÿ îò õðàíåíèÿ ñïèñêà êîìïîíåíòîâ â Entity
* TODO: Èñïîëüçîâàòü ïîäõîä îäèí-êîìïîíåíò-îäíà-ñèñòåìà,
* òîãäà ìîæíî ñîçäàâàòü êîìîçèòíûå êîìïîíåíòû
* TODO: ïîäóìàòü, êàê ñîêðàòèòü âûçîâû êîìïîíåíòîâ, íå èñïîëüçóÿ
* äæåíåðèêè, èëè èñïîëüçóÿ ìèíèìàëüíî. Áîëüøå îïèðàòüñÿ íà Aspect.
* TODO: Äîáàâèòü getAspect â System
* TODO: Ïåðåîáîçâàòü System â Process, âî èçáåæàíèå êîëëèçèé ñ ñèñòåìíûì
* java-êëàññîì System
*/
// public Entity getEntity(int id) {
// for (LinkedList<Component> list : components.values()) {
// for (Object e : map.keySet().toArray()) {
// if (((Entity) e).id == id) {
// return (Entity) e;
// }
// }
// }
// return null;
// }
public List<Entity> getAllEntitiesPossessing( Aspect... aspectList ){
List<Entity> searchResults = new LinkedList();
components.forEach((e, list)->{
list.forEach((c)->{
boolean hasAll = true;
for(Aspect a: aspectList){
if(!c.getAspect().equals(a)) hasAll = false;
java.lang.System.out.println("Entity #"+e.id+(hasAll ? "has" : "has not")+"component with aspect "+a);
}
if(hasAll) searchResults.add(e);
});
});
return searchResults;
}
public void removeEntity(Entity e) {
components.remove(e);
}
public void removeComponent(Entity e, Aspect a) {
List<Component> comps = components.get(e);
for (int i = 0; i < comps.size(); i++) {
if (comps.get(i).getAspect().equals(a)) {
comps.remove(i);
return;
}
}
}
public void addSystem(System s) {
s.init(this);
systems.add(s);
}
public void removeSystem(Class systemClass){
systems.forEach((s)->{
if(s.getClass().equals(systemClass)) systems.remove(s);
});
}
public void render(Graphics g){
systems.forEach((s)->{
if(s instanceof RenderableSystem) {
((RenderableSystem)s).render(g);
}
});
}
public void update(int delta){
systems.forEach((s)->{
s.update(delta);
});
}
//i dunno why this exists
private int nextAviableID(){
Random rand = new Random();
int id = rand.nextInt();
while(usedIDs.contains(id)) id = rand.nextInt();
return id;
}
}

13
src/engine/ecs/RenderableSystem.java

@ -0,0 +1,13 @@
package engine.ecs;
import org.newdawn.slick.Graphics;
/**
* Renderable systems are updated like any other systems, but after being updated, they are rendered as well
* @author Maggistrator
*/
public interface RenderableSystem extends System {
public void render(Graphics g);
}

12
src/engine/ecs/System.java

@ -0,0 +1,12 @@
package engine.ecs;
/**
* System. An superior processor of Components data.
* @author Maggistrator
*/
public interface System {
public void init(EntityManager engine);
public void update(int delta);
}

39
src/game/GameWorld.java

@ -0,0 +1,39 @@
package game;
import engine.ecs.EntityManager;
import org.newdawn.slick.GameContainer;
import org.newdawn.slick.Graphics;
import org.newdawn.slick.SlickException;
import org.newdawn.slick.state.BasicGameState;
import org.newdawn.slick.state.StateBasedGame;
public class GameWorld extends BasicGameState {
private int stateID;
protected EntityManager entityManager = new EntityManager();
public GameWorld(int ID) {
stateID = ID;
}
@Override
public void init(GameContainer arg0, StateBasedGame arg1) throws SlickException {
}
@Override
public void render(GameContainer arg0, StateBasedGame arg1, Graphics arg2) throws SlickException {
entityManager.render(arg2);
}
@Override
public void update(GameContainer arg0, StateBasedGame arg1, int arg2) throws SlickException {
entityManager.update(arg2);
}
@Override
public int getID() {
return stateID;
}
}

27
src/game/components/PositionComponent.java

@ -0,0 +1,27 @@
package game.components;
import engine.ecs.Aspect;
import engine.ecs.Component;
/**
*
* @author Maggistrator
*/
public class PositionComponent implements Component {
public float x;
public float y;
public float rotation;
public PositionComponent(float x, float y, float rotation) {
this.x = x;
this.y = y;
this.rotation = rotation;
}
@Override
public Aspect getAspect() {
return Aspect.POSITION;
}
}

20
src/game/components/SpriteRendererComponent.java

@ -0,0 +1,20 @@
package game.components;
import engine.ecs.Aspect;
import engine.ecs.Component;
import org.newdawn.slick.Image;
/**
*
* @author Maggistrator
*/
public class SpriteRendererComponent implements Component {
public Image image;
@Override
public Aspect getAspect() {
return Aspect.SPRITE;
}
}

44
src/game/systems/SpriteRendererSystem.java

@ -0,0 +1,44 @@
package game.systems;
import engine.ecs.Aspect;
import engine.ecs.Entity;
import engine.ecs.EntityManager;
import engine.ecs.RenderableSystem;
import game.components.PositionComponent;
import game.components.SpriteRendererComponent;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.newdawn.slick.Graphics;
/**
*
* @author Maggistrator
*/
public class SpriteRendererSystem implements RenderableSystem {
Map<SpriteRendererComponent, PositionComponent> sprites = new HashMap<>();
@Override
public void init(EntityManager engine) {
List <Entity> list = engine.getAllEntitiesPossessing(Aspect.SPRITE, Aspect.POSITION);
for(Entity e: list) {
SpriteRendererComponent src = engine.getComponent(e, Aspect.SPRITE);
PositionComponent pc = engine.getComponent(e, Aspect.POSITION);
sprites.put(src, pc);
}
}
@Override
public void render(Graphics g) {
sprites.forEach((sr, pos)->{
g.drawImage(sr.image, pos.x, pos.y);
});
}
@Override
public void update(int delta) {
/* nothing here */
}
}

27
src/test/ecs/ECSTestLauncher.java

@ -0,0 +1,27 @@
package test.ecs;
import org.newdawn.slick.AppGameContainer;
import org.newdawn.slick.GameContainer;
import org.newdawn.slick.SlickException;
import org.newdawn.slick.state.StateBasedGame;
/**
*
* @author Maggistrator
*/
public class ECSTestLauncher {
public static void main(String[] args) throws SlickException {
StateBasedGame game = new StateBasedGame("ECS-Test") {
@Override
public void initStatesList(GameContainer container) throws SlickException {
addState(new ECSWorld(0));
enterState(0);
}
};
AppGameContainer container = new AppGameContainer(game);
container.setDisplayMode(640, 480, false);
container.start();
}
}

54
src/test/ecs/ECSWorld.java

@ -0,0 +1,54 @@
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package test.ecs;
import engine.ecs.Aspect;
import engine.ecs.Entity;
import game.GameWorld;
import game.components.PositionComponent;
import game.components.SpriteRendererComponent;
import game.systems.SpriteRendererSystem;
import org.newdawn.slick.GameContainer;
import org.newdawn.slick.Graphics;
import org.newdawn.slick.Image;
import org.newdawn.slick.SlickException;
import org.newdawn.slick.state.StateBasedGame;
/**
*
* @author Maggistrator
*/
public class ECSWorld extends GameWorld {
Entity spriteEntity ;
public ECSWorld(int ID) {
super(ID);
}
@Override
public void init(GameContainer arg0, StateBasedGame arg1) throws SlickException {
super.init(arg0, arg1);
spriteEntity = entityManager.createNewEntity();
PositionComponent position = new PositionComponent(50, 50, 0);
SpriteRendererComponent spriteRenderer = new SpriteRendererComponent();
spriteRenderer.image = new Image("data/images/test/ecs/eyes.png");
entityManager.addEntity(spriteEntity, position, spriteRenderer);
entityManager.addSystem(new SpriteRendererSystem());
}
@Override
public void render(GameContainer c, StateBasedGame game, Graphics g) throws SlickException {
super.render(c, game, g);
g.drawString("Img: " + ((SpriteRendererComponent)entityManager.getComponent(spriteEntity, Aspect.SPRITE)).image.toString(), 0, 0);
g.drawString("Pos: " + ((PositionComponent)entityManager.getComponent(spriteEntity, Aspect.POSITION)).x + "|"+ ((PositionComponent)entityManager.getComponent(spriteEntity, Aspect.POSITION)).y, 50, 50);
}
@Override
public void update(GameContainer arg0, StateBasedGame arg1, int arg2) throws SlickException {
super.update(arg0, arg1, arg2);
}
}
Loading…
Cancel
Save