浏览代码

Merge branch 'models' into development

Kevin Heinicke 10 年之前
父节点
当前提交
30c2acdbd9
共有 2 个文件被更改,包括 42 次插入0 次删除
  1. 40 0
      js/controller.js
  2. 2 0
      js/models/particle.js

+ 40 - 0
js/controller.js

@@ -5,6 +5,19 @@ var Controller = function(){
 	this.collectibles = [];
     this.views = [];
 	this.snake = new Snake(this.initial_length);
+	var pos0 = {x: -1, y: -1};
+	
+	var higgs = new Particle(pos0);
+	higgs.mass = 125;
+	higgs.type = "Higgs"
+	var electron = new Particle(pos0);
+	electron.mass = .0005;
+	electron.type = "electron";
+
+	this.possible_collectibles = [
+		{collectible: higgs, probability: 3e-3},
+		{collectible: electron, probability: .05}
+		]
 	this.stage = new createjs.Stage("demoCanvas");
 }
 
@@ -14,6 +27,15 @@ Controller.prototype.update_views = function(){
     }
 }
 
+this.prototype.spawn_collectibles = function(){
+	var collectible = Object.create(get_random_element_with_probabilities(this.possible_collectibles));
+	if (!collectible) return;
+	var rnd_pos = this.get_random_position();
+	if (this.is_position_free(rnd_pos)) {
+		collectible.position = rnd_pos;
+	}
+}
+
 Controller.prototype.start_game = function(){
 	// this.session = new Session();
     var c = this;
@@ -69,6 +91,7 @@ Controller.prototype.tick = function(event){
 		this.time = event.time;
         var next_cell = this.get_next_cell_position();
 		this.snake.move(next_cell);
+		this.spawn_collectibles();
         this.update_views();
 	}
 	this.stage.update(event);
@@ -85,3 +108,20 @@ Controller.prototype.get_next_cell_position = function(){
 	if (next_cell.y == this.grid_size.y) next_cell.y = 0;
 	return next_cell;
 }
+
+Controller.prototype.get_random_position = function(){
+	return {x: Math.floor(Math.random()*this.grid_size.x),
+			y: Math.floor(Math.random()*this.grid_size.y)
+		};
+}
+
+var get_random_element_with_probabilities(array){
+	var previous_probability = 0;
+	var rnd = Math.random();
+	for (ind in array){
+		var probability = array[ind].probability;
+		if (rnd  < probability + previous_probability) return array[ind];
+		previous_probability += probability;
+	}
+	return null;
+}

+ 2 - 0
js/models/particle.js

@@ -4,8 +4,10 @@ var Particle = function(position){
 	this.mass = 125;
 	this.charge = 0;
 	this.start_time = 1234;
+	this.decay_time = 4444;
 	this.target = null;
 	this.velocity = null;
 	this.points = 125;
 	this.parent_type = "W";
+	this.decays = [{particles: ["W", "W"], probability: .3 }]
 }