123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294 |
- function vecLen(x,y) {
- return Math.hypot(x,y);
- }
- function abs(a) {
- return Math.abs(a);
- }
- function norm(x,y) {
- var l = vecLen(x,y);
- if(l == 0) return {x:0, y:0};
- return {
- x: x / l,
- y: y / l,
- };
- }
- Systems.urge = function(te) {
-
- var moves = game.components.movable;
- var positions = game.components.position;
- var velocities = game.components.velocity;
- // var accelerations = game.components.acceleration;
- var urges = game.components.urge;
-
- for(var eid in moves) {
- if(moves.hasOwnProperty(eid) &&
- positions.hasOwnProperty(eid) &&
- velocities.hasOwnProperty(eid) &&
- // accelerations.hasOwnProperty(eid) &&
- urges.hasOwnProperty(eid)) {
-
-
- var move = moves[eid];
- var pos = positions[eid];
- var vel = velocities[eid];
- var urge = urges[eid];
- var nextpos = game.getComp(eid, 'nextpos');
-
- if(urge.acc) {
- nextpos.x = pos.x + urge.x * te * urge.acc;
- nextpos.y = pos.y + urge.y * te * urge.acc;
-
-
- }
-
-
-
- }
- }
-
- }
- // Systems.userControl = function() {
- //
- // var ii = game.input;
- //
- //
- // var movers = game.components.controllable;
- //
- // for(var eid in movers) { if(movers.hasOwnProperty(eid)) { var mover = movers[eid];
- //
- // var urge = game.getComp(eid, 'urge');
- // urge.acc = 0;
- // urge.x = 0;
- // urge.y = 0;
- //
- // if(ii.down[37]) {
- // //console.log(ii);
- // urge.x = -1;
- // urge.acc = 1;
- // }
- // if(ii.down[39]) {
- // //console.log(ii);
- // urge.x = 1;
- // urge.acc = 1;
- // }
- // if(ii.down[38]) {
- // //console.log(ii);
- // urge.y = 1;
- // urge.acc = 1;
- // }
- // if(ii.down[40]) {
- // //console.log(ii);
- // urge.y = -1;
- // urge.acc = 1;
- // }
- //
- // // debug
- // // urge.x = -1;
- // // urge.acc = 2;
- //
- // }};
- //
- //
- // }
- Systems.userControl = function() {
-
- var ii = game.input;
-
- var comps = ['controllable'];
-
- runSystem(game.c, comps, function(ent, eid) {
- if(ii.clicked !== null) {
-
- game.addComponent(eid, 'goTo', ptc(ii.clicked));
- }
-
- });
-
- }
- Systems.mapFollows = function() {
-
- var follows = game.components.mapFollows;
-
- var edges = game.map.getEdges(game.mapCenter);
-
- for(var eid in follows) { if(follows.hasOwnProperty(eid)) { var follow = follows[eid];
-
- var pos = game.getComp(eid, 'position');
-
- if(pos.y > edges.t - follow.margin) {
- // move the map up
- game.mapCenter.y += pos.y - (edges.t - follow.margin);
- }
- if(pos.y < edges.b + follow.margin) {
- // move the map down
- game.mapCenter.y -= edges.b + follow.margin - pos.y;
- }
- if(pos.x > edges.r - follow.margin) {
- // move the map up
- game.mapCenter.x += pos.x - (edges.r - follow.margin);
- }
- if(pos.x < edges.l + follow.margin) {
- // move the map down
- game.mapCenter.x -= edges.l + follow.margin - pos.x;
- }
-
- break; // only follow the first guy. really bad temp hack, but whatever
- }}
-
-
- }
- Systems.goTo = function() {
-
- var comps = ['goTo', 'velocity', 'position', 'maxSpeed', 'acceleration', 'state'];
-
- var epsilon = .1;
-
- runSystem(game.components, comps, function(ent) {
- if(ent.goTo == null) return;
- if(vDist(ent.goTo, ent.position) < epsilon) {
- ent.acceleration.x = 0;
- ent.acceleration.y = 0;
- ent.velocity.x = 0;
- ent.velocity.y = 0;
- // neet to set some state for the ai here...
- ent.state.loco = 'standing';
- }
- else {
-
- var a = ent.maxSpeed * 8;
-
- var dir = norm(ent.goTo.x - ent.position.x, ent.goTo.y - ent.position.y);
-
- ent.acceleration.x = a * dir.x;
- ent.acceleration.y = a * dir.y;
-
- ent.state.loco = 'going';
- }
-
- });
-
- };
- Systems.acceleration = function(te) {
-
- var comps = ['acceleration', 'velocity', 'maxSpeed'];
- runSystem(game.components, comps, function(ent) {
-
- var x = ent.velocity.x + ent.acceleration.x * te;
- var y = ent.velocity.y + ent.acceleration.y * te;
- var speed = vecLen(x,y);
-
- // need better integrator
- if(abs(speed) < ent.maxSpeed) {
- ent.velocity.x = x;
- ent.velocity.y = y;
- }
- else {
- var dir = norm(x,y)
- ent.velocity.x = dir.x * ent.maxSpeed;
- ent.velocity.y = dir.y * ent.maxSpeed;
- }
-
- });
-
- };
- Systems.velocity = function(te) {
-
- var comps = ['velocity', 'position', 'nextpos'];
- runSystem(game.components, comps, function(ent) {
-
- // need better integrator
- ent.nextpos.x = ent.position.x + te * ent.velocity.x;
- ent.nextpos.y = ent.position.y + te * ent.velocity.y;
- });
-
- };
- Systems.lookAt = function() {
-
- var comps = ['lookAt', 'sprite'];
- runSystem(game.components, comps, function(ent, eid) {
-
- var dir = game.getComp(eid, ent.lookAt);
-
- if(dir.x == 0 && dir.y == 0) return;
-
- dir = ptNorm(dir);
-
- // need better integrator
- ent.sprite.angle = atan2(
- dir.x,
- dir.y
- );
- });
-
- };
- // you can add your own trip timers in there
- Systems.odometer = function(te) {
-
- var comps = ['nextpos', 'position', 'odometer'];
- runSystem(game.components, comps, function(ent) {
-
- // need better integrator
- var len = vDist(ent.nextpos, ent.position);
- for(var i in ent.odometer) {
- ent.odometer[i] += len;
- };
-
- });
-
- };
- Systems.finalizeMove = function(te) {
- //return;
- var movers = game.components.velocity;
-
- for(var eid in movers) { if(movers.hasOwnProperty(eid)) { var vel = movers[eid];
-
- var pos = game.getComp(eid, 'position');
- var nextpos = game.getComp(eid, 'nextpos');
-
- // references...
- pos.x = nextpos.x;
- pos.y = nextpos.y;
- }};
-
-
- }
|