Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Skeletal slow queue task management stuff.

  • Loading branch information...
commit bcb870f58342421db49fccdbf7b57136fa9cb6e0 1 parent 0120e20
@izb authored
View
10 src/ai/pathfinder.js
@@ -17,7 +17,7 @@ define(function() {
function Node(x,y) {
this.x = x;
this.y = y;
- this.score = 0;
+ this.priority = 0;
}
@@ -434,14 +434,14 @@ define(function() {
continue;
}
- var tscore = current.score + this.cost(current.x,current.y) * this.distance(i,current.x,current.y);
- if (neighbour.closed && tscore>=neighbour.score) {
+ var tscore = current.priority + this.cost(current.x,current.y) * this.distance(i,current.x,current.y);
+ if (neighbour.closed && tscore>=neighbour.priority) {
continue;
}
- if (!neighbour.open || tscore < neighbour.score) {
+ if (!neighbour.open || tscore < neighbour.priority) {
neighbour.cameFrom=current;
- neighbour.score = tscore;
+ neighbour.priority = tscore;
neighbour.fscore = neighbour.gscore+distance2(neighbour.x,neighbour.y,x1,y1);
if (!neighbour.open) {
neighbour.open = true;
View
32 src/snaps.js
@@ -7,6 +7,9 @@ define(['sprites/spritedef', 'sprites/sprite', 'sprites/composite',
/* Plugins */
'plugins/default-plugins',
+ /* Tasks */
+ 'tasks/slowqueue',
+
/* Animation */
'animate/tween',
@@ -19,6 +22,7 @@ define(['sprites/spritedef', 'sprites/sprite', 'sprites/composite',
function(SpriteDef, Sprite, Composite, Keyboard, Mouse, util, StaggeredIsometric,
regPlugins,
+ SlowQueue,
tweens,
ProximityTracker, PathFinder) {
@@ -130,6 +134,8 @@ function(SpriteDef, Sprite, Composite, Keyboard, Mouse, util, StaggeredIsometric
this.activeFX = [];
+ this.taskQueues = [];
+
this.now = 0;
this.epoch = 0; /* 0 in chrome, but moz passes unix time. Epoch will be adjusted on first repaint */
@@ -350,6 +356,18 @@ function(SpriteDef, Sprite, Composite, Keyboard, Mouse, util, StaggeredIsometric
};
/**
+ * @method module:snaps.Snaps#updateTasks
+ * @private
+ */
+ this.updateTasks = function() {
+ var epoch = +new Date();
+ for (var i = _this.taskQueues.length - 1; i >= 0; i--) {
+ _this.taskQueues[i].run();
+ }
+ this.stats.count('updateTasks', (+new Date())-epoch);
+ };
+
+ /**
* @method module:snaps.Snaps#updatePhasers
* @private
*/
@@ -469,6 +487,7 @@ function(SpriteDef, Sprite, Composite, Keyboard, Mouse, util, StaggeredIsometric
_this.requestAnimationFrame(loop);
+ _this.updateTasks();
_this.updateFX();
_this.map.updateLayers(now);
_this.updatePhasers();
@@ -702,6 +721,19 @@ function(SpriteDef, Sprite, Composite, Keyboard, Mouse, util, StaggeredIsometric
return new _this.colliders[type](opts);
};
+ /**
+ * Adds a new task queue. You can add task objects to this queue. Tasks are
+ * run in a time-contrained prioritised way in the game loop.
+ * @method module:snaps.Snaps#createTaskQueue
+ * @param {Number} maxFrameTime The maximum time permitted for this queue to
+ * run in during each frame.
+ * @return {Object} A {@link module:util/slowqueue#SlowQueue|queue object}.
+ */
+ this.createTaskQueue = function(maxFrameTime) {
+ var q = new SlowQueue(maxFrameTime);
+ this.taskQueues.push(q);
+ return q;
+ };
/**
* Create a new camera for use in game. Creating a camera does not
View
56 src/tasks/slowqueue.js
@@ -0,0 +1,56 @@
+/*global define*/
+define(['util/minheap', 'util/uid'], function(MinHeap, uid) {
+
+ 'use strict';
+
+ /**
+ * @module util/slowqueue
+ */
+
+ /**
+ * Implementation of a slow task queue. Task items are dequeued based on an allotted amount
+ * of processing time per frame. Normally you would not construct this directly, but rather you
+ * would use the engine's {@link module:snaps.Snaps#createTaskQueue|createTaskQueue factory method}.
+ * @constructor module:util/slowqueue.SlowQueue
+ * @param {Number} maxFrameTime The max time permitted on each frame for processing
+ * items on the queue. Tasks may take more than one time slot to complete. Queue processing
+ * may exceed this if a task does not honour its promise to return within the given time.
+ */
+ function SlowQueue(maxFrameTime) {
+ /* TODO: Perhaps a started task should always take priority, since you might be able to post
+ * a higher priority task on the same object, and explode its state. */
+ this.queue = new MinHeap();
+ this.maxFrameTime = maxFrameTime;
+ this.id = uid();
+ }
+
+ /**
+ * Add a new task to the queue
+ * @method module:util/slowqueue#addTask
+ * @param {Object} task A task to add
+ * @param {Number} [priority=1] The task priority. High priority tasks will be
+ * done before low ones, even if a low priority task is half-done. Low numbers
+ * are higher priority than high numbers.
+ */
+ SlowQueue.prototype.addTask = function(task, priority) {
+ priority = priority===undefined?1:priority;
+
+ /* TODO: What the heck is a task? */
+
+ this.queue.push({
+ task:task,
+ priority:priority
+ });
+ };
+
+ /**
+ * Runs the task queue. The queue will make a best effort to return within the
+ * configured max frame time.
+ * @method module:util/slowqueue#run
+ */
+ SlowQueue.prototype.run = function() {
+ /* TODO */
+ };
+
+ return SlowQueue;
+});
View
14 src/util/minheap.js
@@ -12,7 +12,7 @@ define(function() {
* <p>
* See {@link http://www.digitaltsunami.net/projects/javascript/minheap/index.html|digitaltsunami.net}
* <p>
- * Modified to expect only to contain objects that expose a 'score'
+ * Modified to expect only to contain objects that expose a 'priority'
* value for comparison.
* @constructor module:util/minheap.MinHeap
*/
@@ -57,12 +57,12 @@ define(function() {
var lIdx = this.left(i);
var rIdx = this.right(i);
var smallest;
- if (lIdx < this.heap.length && (this.heap[lIdx].score < this.heap[i].score)) {
+ if (lIdx < this.heap.length && (this.heap[lIdx].priority < this.heap[i].priority)) {
smallest = lIdx;
} else {
smallest = i;
}
- if (rIdx < this.heap.length && (this.heap[rIdx].score < this.heap[smallest].score)) {
+ if (rIdx < this.heap.length && (this.heap[rIdx].priority < this.heap[smallest].priority)) {
smallest = rIdx;
}
if (i !== smallest) {
@@ -81,7 +81,7 @@ define(function() {
*/
this.siftUp = function(i) {
var p = this.parent(i);
- if (p >= 0 && (this.heap[p].score > this.heap[i].score)) {
+ if (p >= 0 && (this.heap[p].priority > this.heap[i].priority)) {
var temp = this.heap[p];
this.heap[p] = this.heap[i];
this.heap[i] = temp;
@@ -93,7 +93,7 @@ define(function() {
/**
* Place an item in the heap.
* @method module:util/minheap.MinHeap#push
- * @param {Object} item An item that exposes a 'score' property
+ * @param {Object} item An item that exposes a 'priority' property
*/
MinHeap.prototype.push = function(item) {
this.heap.push(item);
@@ -104,7 +104,7 @@ define(function() {
* Pop the minimum valued item off of the heap. The heap is then updated
* to float the next smallest item to the top of the heap.
* @method module:util/minheap.MinHeap#pop
- * @returns {Object} the minimum scored object contained within the heap.
+ * @returns {Object} the minimum priority object contained within the heap.
*/
MinHeap.prototype.pop = function() {
var value;
@@ -124,7 +124,7 @@ define(function() {
* Returns the minimum value contained within the heap. This will
* not remove the value from the heap.
* @method module:util/minheap.MinHeap#pop
- * @returns {Object} the minimum scored object contained within the heap.
+ * @returns {Object} the minimum priority object contained within the heap.
*/
MinHeap.prototype.peek = function() {
return this.heap[0];
Please sign in to comment.
Something went wrong with that request. Please try again.