Skip to content

Instantly share code, notes, and snippets.

@matyklug18
Created February 27, 2022 15:10
Show Gist options
  • Save matyklug18/87f1337663f8684a144f3e74051efa67 to your computer and use it in GitHub Desktop.
Save matyklug18/87f1337663f8684a144f3e74051efa67 to your computer and use it in GitHub Desktop.
//vim: set ft=java
import java.util.*;
class Vector2f {
public float x, y;
public Vector2f(float x, float y) {
this.x = x;
this.y = y;
}
public Vector2f(Vector2f copy) {
this.x = copy.x;
this.y = copy.y;
}
public Vector2f() {
this.x = 0.0f;
this.y = 0.0f;
}
public Vector2f addWith(Vector2f other) {
this.x += other.x;
this.y += other.y;
return this;
}
public Vector2f mulWith(float other) {
this.x *= other;
this.y *= other;
return this;
}
public Vector2f addGet(Vector2f other) {
Vector2f result = new Vector2f(this);
result.x += other.x;
result.y += other.y;
return result;
}
public Vector2f mulGet(float other) {
Vector2f result = new Vector2f(this);
result.x *= other;
result.y *= other;
return result;
}
@Override
public String toString() {
return "(" + this.x + ", " + this.y + ")";
}
}
class Component {
public String name;
public Component(String name) {
this.name = name;
}
}
class RenderComponent extends Component {
public int clr; // "color" is a reserved keyword in processing because *ofc* it is.
public RenderComponent(int clr) {
super("render");
this.clr = clr;
}
public RenderComponent() {
this(color(255, 255, 255));
}
}
class PositionComponent extends Component {
public Vector2f pos;
public PositionComponent(Vector2f pos) {
super("pos");
this.pos = pos;
}
}
class VelocityComponent extends Component {
public Vector2f vel;
public VelocityComponent() {
this(new Vector2f());
}
public VelocityComponent(Vector2f vel) {
super("vel");
this.vel = vel;
}
}
class System {
public List<String> inputs = new ArrayList<>();
public void update(Map<String, Component> comps) {};
public void prepare() {}
public System(String... inputNames) {
for(String s : inputNames) {
inputs.add(s);
}
}
}
class RenderSystem extends System {
public RenderSystem() {
super("render", "pos");
}
public void update(Map<String, Component> comps) {
PositionComponent posC = (PositionComponent)comps.get("pos");
RenderComponent renC = (RenderComponent)comps.get("render");
fill(renC.clr);
ellipse(posC.pos.x-5, posC.pos.y-5, 10, 10);
}
public void prepare() {
clear();
}
}
class VelocitySystem extends System {
public VelocitySystem() {
super("pos", "vel");
}
public void update(Map<String, Component> comps) {
PositionComponent posC = (PositionComponent)comps.get("pos");
VelocityComponent velC = (VelocityComponent)comps.get("vel");
posC.pos.addWith(velC.vel.mulGet(delta/1000.0f));
}
}
class Entity {
public Map<String, Component> comps = new HashMap<>();
public Entity(Component... com) {
for(Component c : com) {
if(comps.containsKey(c.name)) {
throw new IllegalStateException("Duplicate component "+c.name);
}
comps.put(c.name, c);
}
}
}
List<Entity> ens = new ArrayList<>();
List<System> sys = new ArrayList<>();
void addSys(System... syss) {
for(System sy : syss) {
sys.add(sy);
}
}
void addEns(Entity... enss) {
for(Entity en : enss) {
ens.add(en);
}
}
void setup() {
size(400, 600);
clear();
addSys(new RenderSystem(), new VelocitySystem());
addEns(
new Entity(new VelocityComponent(new Vector2f(0.0f, 10.0f)), new RenderComponent(color(255,0,0)), new PositionComponent(new Vector2f(50, 300))),
new Entity(new VelocityComponent(new Vector2f(0.0f, 20.0f)), new RenderComponent(color(0,255,0)), new PositionComponent(new Vector2f(150, 300))),
new Entity(new VelocityComponent(new Vector2f(0.0f, 30.0f)), new RenderComponent(color(0,0,255)), new PositionComponent(new Vector2f(250, 300))),
new Entity(new VelocityComponent(new Vector2f(0.0f, 40.0f)), new RenderComponent(color(255,255,255)), new PositionComponent(new Vector2f(350, 300)))
);
}
int lastTime = 0;
int delta = 0;
void draw() {
delta = millis() - lastTime;
for(System sy : sys) {
sy.prepare();
for(Entity en : ens) {
Map<String, Component> com = new HashMap<>();
boolean satisfy = true;
for(String in : sy.inputs) {
if(en.comps.containsKey(in)) {
com.put(in, en.comps.get(in));
} else {
satisfy = false;
}
}
if(satisfy) {
sy.update(com);
}
}
}
lastTime = millis();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment