Created
January 7, 2015 22:24
-
-
Save TheSeamau5/466ad041931ccc92e826 to your computer and use it in GitHub Desktop.
ECS Example in Dart using OOP
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Point { | |
final num x; | |
final num y; | |
const Point(this.x, this.y); | |
Point operator + (Point p) { | |
return new Point(p.x + x, p.y + y); | |
} | |
Point scale(num factor){ | |
return new Point(x * factor, y * factor); | |
} | |
} | |
compose(f,g){ | |
return (x) => f(g(x)); | |
} | |
abstract class Position { | |
Point position; | |
} | |
abstract class Velocity { | |
Point velocity; | |
} | |
abstract class Life { | |
bool life; | |
} | |
abstract class Mass { | |
num mass; | |
} | |
abstract class Controllability { | |
bool controllability; | |
} | |
abstract class Groundedness { | |
bool groundedness; | |
} | |
abstract class Flying { | |
bool flying; | |
} | |
abstract class Destructability { | |
bool destructability; | |
} | |
abstract class Entity { | |
Entity copy(); | |
} | |
class Mario extends Entity implements Position, Velocity, Life, Mass, Controllability, Groundedness { | |
Point position = new Point(0,0); | |
Point velocity = new Point(0,0); | |
bool life = true; | |
num mass = 20; | |
bool controllability = true; | |
bool groundedness = true; | |
@override Mario copy(){ | |
var marioCopy = new Mario() | |
..position = this.position | |
..velocity = this.velocity | |
..life = this.life | |
..mass = this.mass | |
..controllability = this.controllability | |
..groundedness = this.groundedness; | |
return marioCopy; | |
} | |
} | |
class Goomba extends Entity implements Position, Velocity, Life, Mass, Groundedness { | |
Point position = new Point(20,0); | |
Point velocity = new Point(-1,0); | |
bool life = true; | |
num mass = 20; | |
bool groundedness = true; | |
@override Goomba copy(){ | |
var goombaCopy = new Goomba() | |
..position = this.position | |
..velocity = this.velocity | |
..life = this.life | |
..mass = this.mass | |
..groundedness = this.groundedness; | |
return goombaCopy; | |
} | |
} | |
class LakituCloud extends Entity implements Position, Velocity, Life, Flying { | |
Point position = new Point(0,100); | |
Point velocity = new Point(-2, 0); | |
bool life = true; | |
bool flying = true; | |
@override LakituCloud copy(){ | |
var lakituCloudCopy = new LakituCloud() | |
..position = this.position | |
..velocity = this.velocity | |
..life = this.life | |
..flying = this.flying; | |
return lakituCloudCopy; | |
} | |
} | |
class Block extends Entity implements Position, Destructability { | |
Point position = new Point(0,0); | |
bool destructability = true; | |
@override Block copy(){ | |
var blockCopy = new Block() | |
..position = this.position | |
..destructability = this.destructability; | |
return blockCopy; | |
} | |
} | |
Entity move(Entity entity){ | |
if ((entity is Position) && (entity is Velocity)){ | |
var outputEntity = entity.copy(); | |
outputEntity.position = (entity as Position).position + (entity as Velocity).velocity; | |
return outputEntity; | |
}else{ | |
return entity; | |
} | |
} | |
typedef Entity PointToEntity(Point); | |
PointToEntity applyGravity(Point force){ | |
return (Entity entity){ | |
if ((entity is Mass) && (entity is Velocity)){ | |
var outputEntity = entity.copy(); | |
outputEntity.velocity = (entity as Velocity).velocity + force.scale((entity as Mass).mass); | |
return outputEntity; | |
}else{ | |
return entity; | |
} | |
}; | |
} | |
final update = compose(move, applyGravity(new Point(0, -9.81))); | |
void main() { | |
final Mario mario = new Mario(); | |
final Goomba goomba = new Goomba(); | |
final LakituCloud lakituCloud = new LakituCloud(); | |
final Block block = new Block(); | |
final List<Entity> entities = [mario, goomba, lakituCloud, block]; | |
final List<Entity> updatedEntities = new List.from(entities.map(update)); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment