Commit 42e97f56 authored by Hubert Denkmair's avatar Hubert Denkmair
Browse files

use ObjectPool for food items

parent d0554396
function FoodSprite(texture, decay_rate, food_id, pos_x, pos_y, value) function FoodSprite(texture)
{ {
PIXI.Sprite.call(this, texture); PIXI.Sprite.call(this, texture);
this.food_value = value; this.food_value = 0;
this.item_id = food_id; this.decay_rate = 0;
this.decay_rate = decay_rate; this.item_id = 0;
this.textureRadius = texture.width / 2;
this.anchor.set(0.5); this.anchor.set(0.5);
this.x = pos_x; this.textureRadius = 32;
this.y = pos_y;
this.tint = this.GetRandomTint(); this.tint = this.GetRandomTint();
this.UpdateSize();
} }
FoodSprite.prototype = Object.create(PIXI.Sprite.prototype); FoodSprite.prototype = Object.create(PIXI.Sprite.prototype);
FoodSprite.prototype.SetData = function(decay_rate, food_id, pos_x, pos_y, value)
{
this.decay_rate = decay_rate;
this.food_value = value;
this.item_id = food_id;
this.x = pos_x;
this.y = pos_y;
this.UpdateSize();
};
FoodSprite.prototype.UpdateSize = function() FoodSprite.prototype.UpdateSize = function()
{ {
let size = Math.sqrt(this.food_value) / this.textureRadius; let size = Math.sqrt(this.food_value) / this.textureRadius;
......
...@@ -37,6 +37,10 @@ function GameVisualization(assets, snakeMoveStrategy, container) ...@@ -37,6 +37,10 @@ function GameVisualization(assets, snakeMoveStrategy, container)
this.segmentPool = new ObjectPool(function() { this.segmentPool = new ObjectPool(function() {
return new SnakeSegment(this.txBody); return new SnakeSegment(this.txBody);
}, this, 10000); }, this, 10000);
this.foodItemPool = new ObjectPool(function() {
return new FoodSprite(this.txFood);
}, this, 10000);
} }
GameVisualization.prototype.Run = function() GameVisualization.prototype.Run = function()
...@@ -80,6 +84,7 @@ GameVisualization.prototype.RemoveSnake = function(id) ...@@ -80,6 +84,7 @@ GameVisualization.prototype.RemoveSnake = function(id)
if (id in this.snakes) if (id in this.snakes)
{ {
this.mainStage.removeChild(this.snakes[id].Container); this.mainStage.removeChild(this.snakes[id].Container);
this.snakes[id].Destroy();
delete this.snakes[id]; delete this.snakes[id];
} }
}; };
...@@ -114,12 +119,13 @@ GameVisualization.prototype.HandleTickMessage = function(frame_id) ...@@ -114,12 +119,13 @@ GameVisualization.prototype.HandleTickMessage = function(frame_id)
{ {
for (let food_id in this.foodItems) for (let food_id in this.foodItems)
{ {
if (!this.foodItems[food_id].visible) let item = this.foodItems[food_id];
if (!item.visible)
{ {
delete this.foodItems[food_id]; delete this.foodItems[food_id];
this.foodItemPool.free(item);
} }
} }
this.foodMap.CleanUp(); this.foodMap.CleanUp();
} }
...@@ -155,7 +161,8 @@ GameVisualization.prototype.HandleWorldUpdateMessage = function(data) ...@@ -155,7 +161,8 @@ GameVisualization.prototype.HandleWorldUpdateMessage = function(data)
GameVisualization.prototype.AddFood = function(food_id, pos_x, pos_y, value) GameVisualization.prototype.AddFood = function(food_id, pos_x, pos_y, value)
{ {
let sprite = new FoodSprite(this.txFood, this.food_decay_rate, food_id, pos_x, pos_y, value); let sprite = this.foodItemPool.get();
sprite.SetData(this.food_decay_rate, food_id, pos_x, pos_y, value);
this.foodItems[food_id] = sprite; this.foodItems[food_id] = sprite;
this.foodMap.AddSprite(sprite); this.foodMap.AddSprite(sprite);
}; };
...@@ -180,7 +187,16 @@ GameVisualization.prototype.HandleFoodConsumedMessage = function(food_id, consum ...@@ -180,7 +187,16 @@ GameVisualization.prototype.HandleFoodConsumedMessage = function(food_id, consum
if (food_id in this.foodItems) if (food_id in this.foodItems)
{ {
let sprite = this.foodItems[food_id]; let sprite = this.foodItems[food_id];
this.snakes[consumer_id].Eat(sprite); if (consumer_id in this.snakes)
{
this.snakes[consumer_id].Eat(sprite);
}
else
{
this.foodItems[food_id].visible = false;
delete this.foodItems[food_id];
this.foodItemPool.free(sprite);
}
} }
}; };
......
...@@ -29,6 +29,27 @@ function Snake(headTexture, segmentPool, name, colorScheme, world_size_x, world_ ...@@ -29,6 +29,27 @@ function Snake(headTexture, segmentPool, name, colorScheme, world_size_x, world_
this.Container.addChild(this._nameSprite); this.Container.addChild(this._nameSprite);
} }
Snake.prototype.Destroy = function()
{
while (this._segments.length>0)
{
let segment = this._segments.pop();
this._segmentContainer.removeChild(segment.GetSprite());
this._segmentPool.free(segment);
}
while (this._foodContainer.children.length>0)
{
let foodItem = this._foodContainer.children[0];
foodItem.visible = false; // will be removed with decayed food
this._foodContainer.removeChild(foodItem);
}
this._foodContainer.destroy();
this._segmentContainer.destroy();
this.Container.destroy();
};
Snake.prototype.SetData = function(data) Snake.prototype.SetData = function(data)
{ {
this.heading = data.heading; this.heading = data.heading;
...@@ -163,8 +184,7 @@ Snake.prototype.AnimateEat = function() ...@@ -163,8 +184,7 @@ Snake.prototype.AnimateEat = function()
if (dist < radius) if (dist < radius)
{ {
food.visible = false; food.visible = false;
this._foodContainer.removeChildAt(i); this._foodContainer.removeChild(food);
return; // FIXME this aborts eating for this animation cycle
} }
let factor = (dist-food.speed) / dist; let factor = (dist-food.speed) / dist;
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment