function TournamentSim(config){ var self = this; self.id = config.id; // APP var app = new PIXI.Application(500, 500, {transparent:true, resolution:2}); self.dom = app.view; // DOM self.dom.className = "object"; self.dom.style.width = 500; self.dom.style.height = 500; //self.dom.classList.add("fader"); self.dom.style.left = config.x+"px"; self.dom.style.top = config.y+"px"; //self.dom.style.border = "1px solid rgba(0,0,0,0.2)"; // CREATE A RING OF AGENTS var AGENTS = [ {strategy:"all_c", count:15}, {strategy:"all_d", count:5}, {strategy:"grim", count:0}, {strategy:"tft", count:5}, ]; var _convertCountToArray = function(countList){ var array = []; for(var i=0; i0) self.agents[_playIndex-1].dehighlightConnections(); if(_playIndex=1){ _tweenTimer = 0; self.STAGE = STAGE_REST; } } }); // PLAY A TOURNAMENT self._startPlay = function(){ self.STAGE=STAGE_PLAY; }; subscribe("tournament/play", self._startPlay); self._startEliminate = function(){ self.STAGE=STAGE_ELIMINATE; }; subscribe("tournament/eliminate", self._startEliminate); self._startReproduce = function(){ self.STAGE=STAGE_REPRODUCE; }; subscribe("tournament/reproduce", self._startReproduce); // Add... self.add = function(INSTANT){ return _add(self); }; // Remove... self.remove = function(INSTANT){ return _remove(self); }; } /////////////////////////////////////////////////////// /////////////////////////////////////////////////////// /////////////////////////////////////////////////////// function TournamentConnection(config){ var self = this; // Connect from & to self.from = config.from; self.to = config.to; self.from.connections.push(self); self.to.connections.push(self); // Graphics! var g = new PIXI.Container(); var gray = PIXI.Sprite.fromImage("assets/connection.png"); var gold = PIXI.Sprite.fromImage("assets/connection_gold.png"); gray.height = 1; gold.height = 2; gray.anchor.y = gold.anchor.y = 0.5; g.addChild(gray); g.addChild(gold); self.graphics = g; // Highlight or no? self.highlight = function(){ gray.visible = false; gold.visible = true; }; self.dehighlight = function(){ gray.visible = true; gold.visible = false; }; self.dehighlight(); // Stretch dat bad boy self.updateGraphics = function(){ var f = self.from.graphics; var t = self.to.graphics; var dx = t.x-f.x; var dy = t.y-f.y; var a = Math.atan2(dy,dx); var dist = Math.sqrt(dx*dx+dy*dy); g.x = f.x; g.y = f.y; g.rotation = a; gray.width = gold.width = dist; }; self.updateGraphics(); // KILL self.IS_DEAD = false; self.kill = function(){ if(self.IS_DEAD) return; self.IS_DEAD = true; self.graphics.parent.removeChild(self.graphics); // remove self's graphics }; }; /////////////////////////////////////////////////////// /////////////////////////////////////////////////////// /////////////////////////////////////////////////////// function TournamentAgent(config){ var self = this; self.strategyName = config.strategy; self.tournament = config.tournament; self.angle = config.angle; self.gotoAngle = self.angle; // Connections self.connections = []; self.highlightConnections = function(){ for(var i=0;i250) body.scale.x*=-1; body.anchor.x = 0.5; body.anchor.y = 0.75; g.addChild(body); // Score! var textStyle = new PIXI.TextStyle({ fontFamily: "FuturaHandwritten", fontSize: 16, fill: "#444" }); var scoreText = new PIXI.Text("", textStyle); scoreText.anchor.x = 0.5; g.addChild(scoreText); self.updateScore = function(){ scoreText.visible = true; scoreText.text = self.coins; }; self.updateScore(); scoreText.visible = false; /*subscribe("tournament/play",function(){ scoreText.visible = false; });*/ subscribe("tournament/reproduce",function(){ scoreText.visible = false; }); // What's the play logic? var LogicClass = window["Logic_"+self.strategyName]; self.logic = new LogicClass(); self.play = function(){ return self.logic.play(); }; self.remember = function(other){ self.logic.remember(other); }; // Reset! self.resetCoins = function(){ self.coins = 0; // reset coins; self.updateScore(); } self.resetLogic = function(){ self.logic = new LogicClass(); // reset logic }; // Tween angle... self.tweenAngle = function(t){ self.angle = self.gotoAngle*t + self.angle*(1-t); }; self.updatePosition = function(){ g.x = Math.cos(self.angle)*200 + 250; g.y = Math.sin(self.angle)*200 + 250; scoreText.x = -Math.cos(self.angle)*40; scoreText.y = -Math.sin(self.angle)*48 - 22; }; self.updatePosition(); // KILL self.kill = function(){ // KILL ALL CONNECTIONS for(var i=0;i