Commit 1da85e01 authored by Hubert Denkmair's avatar Hubert Denkmair
Browse files

maybe make deleting food more performant

parent 41118258
......@@ -11,6 +11,7 @@ function GameVisualization(assets, snakeMoveStrategy, container)
this.world_size_x = 1024;
this.world_size_y = 1024;
this.food_decay_rate = 0.001;
this.foodItems = {};
this.colorSchemes = [
[ 0xFF0000, 0xFF0000, 0xFF0000, 0xFF0000, 0xA00000, 0xA00000, 0xA00000, 0xA00000 ],
......@@ -102,6 +103,19 @@ GameVisualization.prototype.HandleTickMessage = function(frame_id)
food.Decay(32);
}, this);
this.nextFoodDecayRow %= 64;
if (this.nextFoodDecayRow == 0)
{
for (let food_id in this.foodItems)
{
if (!this.foodItems[food_id].visible)
{
delete this.foodItems[food_id];
}
}
this.foodMap.CleanUp();
}
};
GameVisualization.prototype.HandlePlayerInfoMessage = function(player_id)
......@@ -131,10 +145,17 @@ GameVisualization.prototype.HandleWorldUpdateMessage = function(data)
for (let id in data.food)
{
let food = data.food[id];
this.foodMap.AddSprite(new FoodSprite(this.txFood, this.food_decay_rate, food.id, food.pos_x, food.pos_y, food.value));
this.AddFood(food.id, food.pos_x, food.pos_y, food.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);
this.foodItems[food_id] = sprite;
this.foodMap.AddSprite(sprite);
};
GameVisualization.prototype.HandleBotSpawnMessage = function(bot)
{
this.CreateSnake(bot);
......@@ -147,21 +168,25 @@ GameVisualization.prototype.HandleBotKilledMessage = function(killer_id, victim_
GameVisualization.prototype.HandleFoodSpawnMessage = function(food_id, pos_x, pos_y, value)
{
this.foodMap.AddSprite(new FoodSprite(this.txFood, this.food_decay_rate, food_id, pos_x, pos_y, value));
this.AddFood(food_id, pos_x, pos_y, value);
};
GameVisualization.prototype.HandleFoodConsumedMessage = function(food_id, consumer_id)
{
let sprite = this.foodMap.RemoveItem(food_id);
if (sprite)
if (food_id in this.foodItems)
{
let sprite = this.foodItems[food_id];
this.snakes[consumer_id].Eat(sprite);
}
};
GameVisualization.prototype.HandleFoodDecayedMessage = function(food_id)
{
this.foodMap.RemoveItem(food_id);
if (food_id in this.foodItems)
{
let sprite = this.foodItems[food_id];
sprite.visible = false;
}
};
GameVisualization.prototype.HandleBotMovedMessage = function(bot_id, segment_data, length, segment_radius)
......
......@@ -23,11 +23,6 @@ ParticleGeoMap.prototype.AddSprite = function(sprite)
{
this._itemIdKeyMap[sprite.item_id] = key;
}
if (!(key in this._geoMap))
{
console.log("key ", key, " is not in geo map!");
this._geoMap[key] = new PIXI.Container();
}
this._geoMap[key].addChild(sprite);
};
......@@ -53,6 +48,27 @@ ParticleGeoMap.prototype.RemoveItem = function(item_id)
}
};
ParticleGeoMap.prototype.CleanUp = function()
{
for (let key in this._geoMap)
{
let container = this._geoMap[key];
for (let i in container.children)
{
let child = container.children[i];
if (child.visible)
{
continue;
}
let key = this._itemIdKeyMap[child.item_id];
delete this._itemIdKeyMap[key];
container.removeChild(child);
}
}
};
ParticleGeoMap.prototype.Iterate = function(callback)
{
for (let key in this._geoMap)
......@@ -130,7 +146,6 @@ ParticleGeoMap.prototype.FindContainersInRadius = function(x, y, radiusX, radius
}
}
}
};
ParticleGeoMap.prototype.FindItemsInRadius = function(x, y, radius, callback, context)
......
......@@ -229,6 +229,7 @@ Snake.prototype.AnimateEat = function()
if (dist < radius)
{
food.visible = false;
this._foodContainer.removeChildAt(i);
return; // FIXME this aborts eating for this animation cycle
}
......
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