Commit 840dd18c authored by Hubert Denkmair's avatar Hubert Denkmair
Browse files

optimize food visibility

parent 37bf75b6
......@@ -20,10 +20,11 @@ FoodSprite.prototype.SetData = function(decay_rate, food_id, pos_x, pos_y, value
this.y = pos_y;
this.tint = this.GetRandomTint();
this.visible = true;
this.UpdateSize();
this.request_garbage_collect = false;
this.UpdateScale();
};
FoodSprite.prototype.UpdateSize = function()
FoodSprite.prototype.UpdateScale = function()
{
let size = Math.sqrt(this.food_value) / this.textureRadius;
this.scale.x = size;
......@@ -36,11 +37,12 @@ FoodSprite.prototype.Decay = function(cycles)
{
if (!cycles) { cycles = 1; }
this.food_value -= cycles*this.decay_rate;
this.UpdateSize();
this.UpdateScale();
}
if (this.food_value <= 0.05)
if (this.food_value < 0)
{
this.visible = false;
this.request_garbage_collect = true;
}
};
......
......@@ -133,16 +133,14 @@ GameVisualization.prototype.HandleTickMessage = function(frame_id)
{
let item = this.foodItems[food_id];
item.Decay(nth);
if (!item.visible)
if (item.request_garbage_collect)
{
delete this.foodItems[food_id];
this.foodItemPool.free(item);
}
}
this.foodMap.CleanUp();
this.foodMap.GarbageCollect();
}
this.UpdateStagePosition();
};
GameVisualization.prototype.HandleWorldUpdateMessage = function(data)
......@@ -256,24 +254,22 @@ GameVisualization.prototype.FollowDbId = function(db_id)
GameVisualization.prototype.UpdateStagePosition = function()
{
if (this.ego_id in this.snakes)
{
/*let egoSnake = this.snakes[this.ego_id];
let egoX = egoSnake.GetHeadX();
let egoY = egoSnake.GetHeadY();
let transX = (this.app.renderer.width/2) - egoX;
let transY = (this.app.renderer.height/2) - egoY;
this.app.stage.setTransform(transX, transY);
if (this.foodMap)
if ((++this.updateVisibilityCounter < 25) || (!this.foodMap))
{
this.foodMap.Update(egoX, egoY, this.app.renderer.width, this.app.renderer.height);
} */
} else {
this.app.stage.setTransform(0,0);
if (this.foodMap)
{
this.foodMap.Update(this.world_size_x/2, this.world_size_y/2, this.world_size_x, this.world_size_y);
}
return;
}
this.updateVisibilityCounter = 0;
const center = this.viewport.center;
const width = this.viewport.right - this.viewport.left;
const height = this.viewport.bottom - this.viewport.top;
this.foodMap.Update(center.x, center.y, width, height);
const minimumVisibleFoodSize = 0.5 / this.viewport.scale.x;
console.log(minimumVisibleFoodSize);
this.foodMap.Iterate(function(foodSprite) {
foodSprite.visible = foodSprite.food_value > minimumVisibleFoodSize;
});
};
......@@ -26,41 +26,19 @@ ParticleGeoMap.prototype.AddSprite = function(sprite)
this._geoMap[key].addChild(sprite);
};
ParticleGeoMap.prototype.RemoveItem = function(item_id)
{
if (!(item_id in this._itemIdKeyMap))
{
return;
}
let key = this._itemIdKeyMap[item_id];
delete this._itemIdKeyMap[key];
let container = this._geoMap[key];
for (let i in container.children)
{
let child = container.children[i];
if (child.item_id == item_id)
{
container.removeChild(child);
return child;
}
}
};
ParticleGeoMap.prototype.CleanUp = function()
ParticleGeoMap.prototype.GarbageCollect = function()
{
for (let container of this._geoMap)
{
this.RemoveInvisibleFromContainer(container);
this.GarbageCollectContainer(container);
}
};
ParticleGeoMap.prototype.RemoveInvisibleFromContainer = function(container)
ParticleGeoMap.prototype.GarbageCollectContainer = function(container)
{
for (let child of container.children)
{
if (!child.visible)
if (child.request_garbage_collect)
{
let key = this._itemIdKeyMap[child.item_id];
delete this._itemIdKeyMap[key];
......@@ -81,18 +59,6 @@ ParticleGeoMap.prototype.Iterate = function(callback)
}
};
ParticleGeoMap.prototype.IterateRow = function(row, callback, context)
{
for (let i=row*this._numTilesX; i<(row+1)*this._numTilesX; i++)
{
let container = this._geoMap[i];
for (let i in container.children)
{
callback.call(context, container.children[i]);
}
}
};
ParticleGeoMap.prototype.Update = function(x, y, width, height)
{
if ( (this._last.x==x) && (this._last.y==y) && (this._last.width==width) && (this._last.height==height) )
......
Supports Markdown
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