2017-04-20 19:17:05 -04:00
|
|
|
const test = require('tap').test;
|
|
|
|
const RenderedTarget = require('../../src/sprites/rendered-target');
|
|
|
|
const Sprite = require('../../src/sprites/sprite');
|
|
|
|
const Runtime = require('../../src/engine/runtime');
|
|
|
|
const FakeRenderer = require('../fixtures/fake-renderer');
|
2016-10-03 10:16:43 -04:00
|
|
|
|
2017-04-20 19:17:05 -04:00
|
|
|
test('clone effects', t => {
|
2016-10-03 10:16:43 -04:00
|
|
|
// Create two clones and ensure they have different graphic effect objects.
|
|
|
|
// Regression test for Github issue #224
|
2017-04-20 19:17:05 -04:00
|
|
|
const spr = new Sprite();
|
|
|
|
const a = new RenderedTarget(spr, null);
|
|
|
|
const b = new RenderedTarget(spr, null);
|
2016-10-03 10:16:43 -04:00
|
|
|
t.ok(a.effects !== b.effects);
|
|
|
|
t.end();
|
|
|
|
});
|
2017-03-06 16:15:04 -05:00
|
|
|
|
2017-04-20 19:17:05 -04:00
|
|
|
test('setxy', t => {
|
|
|
|
const s = new Sprite();
|
|
|
|
const r = new Runtime();
|
|
|
|
const a = new RenderedTarget(s, r);
|
|
|
|
const renderer = new FakeRenderer();
|
2017-04-07 14:50:13 -04:00
|
|
|
a.renderer = renderer;
|
|
|
|
a.setXY(123, 321, true);
|
|
|
|
t.equals(a.x, 123);
|
|
|
|
t.equals(a.y, 321);
|
|
|
|
t.end();
|
|
|
|
});
|
|
|
|
|
2017-04-20 19:17:05 -04:00
|
|
|
test('direction', t => {
|
|
|
|
const s = new Sprite();
|
|
|
|
const r = new Runtime();
|
|
|
|
const a = new RenderedTarget(s, r);
|
|
|
|
const renderer = new FakeRenderer();
|
2017-04-07 14:50:13 -04:00
|
|
|
a.renderer = renderer;
|
|
|
|
a.setDirection(123);
|
|
|
|
t.equals(a._getRenderedDirectionAndScale().direction, 123);
|
|
|
|
t.end();
|
|
|
|
});
|
|
|
|
|
2017-04-20 19:17:05 -04:00
|
|
|
test('setSay', t => {
|
|
|
|
const s = new Sprite();
|
|
|
|
const r = new Runtime();
|
|
|
|
const a = new RenderedTarget(s, r);
|
|
|
|
const renderer = new FakeRenderer();
|
2017-04-07 14:50:13 -04:00
|
|
|
a.renderer = renderer;
|
|
|
|
a.setSay();
|
|
|
|
a.setSay('types not specified', 'message');
|
|
|
|
t.end();
|
|
|
|
});
|
|
|
|
|
2017-04-20 19:17:05 -04:00
|
|
|
test('setVisible', t => {
|
|
|
|
const s = new Sprite();
|
|
|
|
const r = new Runtime();
|
|
|
|
const a = new RenderedTarget(s, r);
|
|
|
|
const renderer = new FakeRenderer();
|
2017-04-07 14:50:13 -04:00
|
|
|
a.renderer = renderer;
|
|
|
|
a.setVisible(true);
|
|
|
|
t.end();
|
|
|
|
});
|
|
|
|
|
2017-04-20 19:17:05 -04:00
|
|
|
test('setSize', t => {
|
|
|
|
const s = new Sprite();
|
|
|
|
const r = new Runtime();
|
|
|
|
const a = new RenderedTarget(s, r);
|
|
|
|
const renderer = new FakeRenderer();
|
2017-04-07 14:50:13 -04:00
|
|
|
a.renderer = renderer;
|
|
|
|
a.setSize(123);
|
|
|
|
t.equals(a._getRenderedDirectionAndScale().scale[0], 123);
|
|
|
|
t.end();
|
|
|
|
});
|
|
|
|
|
2017-04-20 19:17:05 -04:00
|
|
|
test('set and clear effects', t => {
|
|
|
|
const s = new Sprite();
|
|
|
|
const r = new Runtime();
|
|
|
|
const a = new RenderedTarget(s, r);
|
|
|
|
const renderer = new FakeRenderer();
|
2017-04-07 14:50:13 -04:00
|
|
|
a.renderer = renderer;
|
2017-08-26 13:14:26 -04:00
|
|
|
for (const effect in a.effects) {
|
2017-04-07 14:50:13 -04:00
|
|
|
a.setEffect(effect, 1);
|
|
|
|
t.equals(a.effects[effect], 1);
|
|
|
|
}
|
|
|
|
a.clearEffects();
|
2017-08-26 13:14:26 -04:00
|
|
|
for (const effect in a.effects) {
|
2017-04-07 14:50:13 -04:00
|
|
|
t.equals(a.effects[effect], 0);
|
|
|
|
}
|
|
|
|
t.end();
|
|
|
|
});
|
|
|
|
|
2017-04-20 19:17:05 -04:00
|
|
|
test('setCostume', t => {
|
|
|
|
const o = new Object();
|
|
|
|
const s = new Sprite();
|
|
|
|
const r = new Runtime();
|
2017-04-07 14:50:13 -04:00
|
|
|
s.costumes = [o];
|
2017-04-20 19:17:05 -04:00
|
|
|
const a = new RenderedTarget(s, r);
|
|
|
|
const renderer = new FakeRenderer();
|
2017-04-07 14:50:13 -04:00
|
|
|
a.renderer = renderer;
|
|
|
|
a.setCostume(0);
|
|
|
|
t.end();
|
|
|
|
});
|
|
|
|
|
2017-05-16 11:01:50 -04:00
|
|
|
test('deleteCostume', t => {
|
|
|
|
const o1 = {id: 1};
|
|
|
|
const o2 = {id: 2};
|
|
|
|
const o3 = {id: 3};
|
2017-05-18 13:56:14 -04:00
|
|
|
const o4 = {id: 4};
|
|
|
|
const o5 = {id: 5};
|
2017-05-16 11:01:50 -04:00
|
|
|
|
|
|
|
const s = new Sprite();
|
|
|
|
const r = new Runtime();
|
|
|
|
s.costumes = [o1, o2, o3];
|
|
|
|
const a = new RenderedTarget(s, r);
|
|
|
|
const renderer = new FakeRenderer();
|
|
|
|
a.renderer = renderer;
|
|
|
|
|
2017-05-18 13:56:14 -04:00
|
|
|
// x* Costume 1 * Costume 2
|
|
|
|
// Costume 2 => Costume 3
|
|
|
|
// Costume 3
|
2017-05-16 11:01:50 -04:00
|
|
|
a.setCostume(0);
|
|
|
|
a.deleteCostume(0);
|
|
|
|
t.deepEqual(a.sprite.costumes, [o2, o3]);
|
|
|
|
t.equals(a.currentCostume, 0);
|
|
|
|
|
2017-05-18 13:56:14 -04:00
|
|
|
// Costume 1 Costume 1
|
|
|
|
// x* Costume 2 => * Costume 3
|
|
|
|
// Costume 3
|
2017-05-16 11:01:50 -04:00
|
|
|
a.sprite.costumes = [o1, o2, o3];
|
|
|
|
a.setCostume(1);
|
|
|
|
a.deleteCostume(1);
|
|
|
|
t.deepEqual(a.sprite.costumes, [o1, o3]);
|
|
|
|
t.equals(a.currentCostume, 1);
|
|
|
|
|
2017-05-18 13:56:14 -04:00
|
|
|
// Costume 1 Costume 1
|
|
|
|
// Costume 2 => * Costume 2
|
|
|
|
// x* Costume 3
|
2017-05-16 11:01:50 -04:00
|
|
|
a.sprite.costumes = [o1, o2, o3];
|
|
|
|
a.setCostume(2);
|
|
|
|
a.deleteCostume(2);
|
|
|
|
t.deepEqual(a.sprite.costumes, [o1, o2]);
|
|
|
|
t.equals(a.currentCostume, 1);
|
|
|
|
|
|
|
|
// Refuses to delete only costume
|
|
|
|
a.sprite.costumes = [o1];
|
|
|
|
a.setCostume(0);
|
|
|
|
a.deleteCostume(0);
|
|
|
|
t.deepEqual(a.sprite.costumes, [o1]);
|
|
|
|
t.equals(a.currentCostume, 0);
|
|
|
|
|
2017-05-18 13:56:14 -04:00
|
|
|
// Costume 1 Costume 1
|
|
|
|
// x Costume 2 Costume 3
|
|
|
|
// Costume 3 => * Costume 4
|
|
|
|
// * Costume 4 Costume 5
|
|
|
|
// Costume 5
|
|
|
|
a.sprite.costumes = [o1, o2, o3, o4, o5];
|
|
|
|
a.setCostume(3);
|
|
|
|
a.deleteCostume(1);
|
|
|
|
t.deepEqual(a.sprite.costumes, [o1, o3, o4, o5]);
|
|
|
|
t.equals(a.currentCostume, 2);
|
|
|
|
|
|
|
|
// Costume 1 Costume 1
|
|
|
|
// * Costume 2 * Costume 2
|
|
|
|
// Costume 3 => Costume 3
|
|
|
|
// x Costume 4 Costume 5
|
|
|
|
// Costume 5
|
|
|
|
a.sprite.costumes = [o1, o2, o3, o4, o5];
|
|
|
|
a.setCostume(1);
|
|
|
|
a.deleteCostume(3);
|
|
|
|
t.deepEqual(a.sprite.costumes, [o1, o2, o3, o5]);
|
|
|
|
t.equals(a.currentCostume, 1);
|
|
|
|
|
|
|
|
// Costume 1 Costume 1
|
|
|
|
// * Costume 2 * Costume 2
|
|
|
|
// Costume 3 => Costume 3
|
|
|
|
// Costume 4 Costume 4
|
|
|
|
// x Costume 5
|
|
|
|
a.sprite.costumes = [o1, o2, o3, o4, o5];
|
|
|
|
a.setCostume(1);
|
|
|
|
a.deleteCostume(4);
|
|
|
|
t.deepEqual(a.sprite.costumes, [o1, o2, o3, o4]);
|
|
|
|
t.equals(a.currentCostume, 1);
|
2017-05-16 11:01:50 -04:00
|
|
|
t.end();
|
|
|
|
});
|
|
|
|
|
|
|
|
test('deleteSound', t => {
|
|
|
|
const o1 = {id: 1};
|
|
|
|
const o2 = {id: 2};
|
|
|
|
const o3 = {id: 3};
|
|
|
|
|
|
|
|
const s = new Sprite();
|
|
|
|
const r = new Runtime();
|
|
|
|
s.sounds = [o1, o2, o3];
|
|
|
|
const a = new RenderedTarget(s, r);
|
|
|
|
const renderer = new FakeRenderer();
|
|
|
|
a.renderer = renderer;
|
|
|
|
|
|
|
|
a.deleteSound(0);
|
|
|
|
t.deepEqual(a.sprite.sounds, [o2, o3]);
|
|
|
|
|
2017-05-18 13:29:44 -04:00
|
|
|
// Allows deleting the only sound
|
2017-05-16 11:01:50 -04:00
|
|
|
a.sprite.sounds = [o1];
|
|
|
|
a.deleteSound(0);
|
2017-05-18 13:29:44 -04:00
|
|
|
t.deepEqual(a.sprite.sounds, []);
|
2017-05-16 11:01:50 -04:00
|
|
|
|
|
|
|
t.end();
|
|
|
|
});
|
|
|
|
|
2017-04-20 19:17:05 -04:00
|
|
|
test('setRotationStyle', t => {
|
|
|
|
const s = new Sprite();
|
|
|
|
const r = new Runtime();
|
|
|
|
const a = new RenderedTarget(s, r);
|
|
|
|
const renderer = new FakeRenderer();
|
2017-04-07 14:50:13 -04:00
|
|
|
a.renderer = renderer;
|
|
|
|
a.setRotationStyle(RenderedTarget.ROTATION_STYLE_NONE);
|
|
|
|
t.end();
|
|
|
|
});
|
|
|
|
|
2017-04-20 19:17:05 -04:00
|
|
|
test('getBounds', t => {
|
|
|
|
const s = new Sprite();
|
|
|
|
const r = new Runtime();
|
|
|
|
const renderer = new FakeRenderer();
|
2017-04-07 14:50:13 -04:00
|
|
|
r.attachRenderer(renderer);
|
2017-04-20 19:17:05 -04:00
|
|
|
const a = new RenderedTarget(s, r);
|
2017-04-07 14:50:13 -04:00
|
|
|
a.renderer = renderer;
|
|
|
|
t.equals(a.getBounds().top, 0);
|
|
|
|
a.setXY(241, 241);
|
|
|
|
t.equals(a.getBounds().top, 241);
|
|
|
|
t.end();
|
|
|
|
});
|
|
|
|
|
2017-04-20 19:17:05 -04:00
|
|
|
test('isTouchingPoint', t => {
|
|
|
|
const s = new Sprite();
|
|
|
|
const r = new Runtime();
|
|
|
|
const renderer = new FakeRenderer();
|
2017-04-07 14:50:13 -04:00
|
|
|
r.attachRenderer(renderer);
|
2017-04-20 19:17:05 -04:00
|
|
|
const a = new RenderedTarget(s, r);
|
2017-04-07 14:50:13 -04:00
|
|
|
a.renderer = renderer;
|
|
|
|
t.equals(a.isTouchingPoint(), true);
|
|
|
|
t.end();
|
|
|
|
});
|
|
|
|
|
2017-04-20 19:17:05 -04:00
|
|
|
test('isTouchingEdge', t => {
|
|
|
|
const s = new Sprite();
|
|
|
|
const r = new Runtime();
|
|
|
|
const renderer = new FakeRenderer();
|
2017-04-07 14:50:13 -04:00
|
|
|
r.attachRenderer(renderer);
|
2017-04-20 19:17:05 -04:00
|
|
|
const a = new RenderedTarget(s, r);
|
2017-04-07 14:50:13 -04:00
|
|
|
a.renderer = renderer;
|
|
|
|
t.equals(a.isTouchingEdge(), false);
|
|
|
|
a.setXY(1000, 1000);
|
|
|
|
t.equals(a.isTouchingEdge(), true);
|
|
|
|
t.end();
|
|
|
|
});
|
|
|
|
|
2017-04-20 19:17:05 -04:00
|
|
|
test('isTouchingSprite', t => {
|
|
|
|
const s = new Sprite();
|
|
|
|
const r = new Runtime();
|
|
|
|
const renderer = new FakeRenderer();
|
2017-04-07 14:50:13 -04:00
|
|
|
r.attachRenderer(renderer);
|
2017-04-20 19:17:05 -04:00
|
|
|
const a = new RenderedTarget(s, r);
|
2017-04-07 14:50:13 -04:00
|
|
|
a.renderer = renderer;
|
|
|
|
t.equals(a.isTouchingSprite('fake'), false);
|
|
|
|
t.end();
|
|
|
|
});
|
|
|
|
|
2017-04-20 19:17:05 -04:00
|
|
|
test('isTouchingColor', t => {
|
|
|
|
const s = new Sprite();
|
|
|
|
const r = new Runtime();
|
|
|
|
const renderer = new FakeRenderer();
|
2017-04-07 14:50:13 -04:00
|
|
|
r.attachRenderer(renderer);
|
2017-04-20 19:17:05 -04:00
|
|
|
const a = new RenderedTarget(s, r);
|
2017-04-07 14:50:13 -04:00
|
|
|
a.renderer = renderer;
|
|
|
|
t.equals(a.isTouchingColor(), false);
|
|
|
|
t.end();
|
|
|
|
});
|
|
|
|
|
2017-04-20 19:17:05 -04:00
|
|
|
test('colorIsTouchingColor', t => {
|
|
|
|
const s = new Sprite();
|
|
|
|
const r = new Runtime();
|
|
|
|
const renderer = new FakeRenderer();
|
2017-04-07 14:50:13 -04:00
|
|
|
r.attachRenderer(renderer);
|
2017-04-20 19:17:05 -04:00
|
|
|
const a = new RenderedTarget(s, r);
|
2017-04-07 14:50:13 -04:00
|
|
|
a.renderer = renderer;
|
|
|
|
t.equals(a.colorIsTouchingColor(), false);
|
|
|
|
t.end();
|
|
|
|
});
|
|
|
|
|
2017-04-20 19:17:05 -04:00
|
|
|
test('layers', t => {
|
|
|
|
const s = new Sprite();
|
|
|
|
const r = new Runtime();
|
|
|
|
const renderer = new FakeRenderer();
|
|
|
|
const o = new Object();
|
2017-04-07 14:50:13 -04:00
|
|
|
r.attachRenderer(renderer);
|
2017-04-20 19:17:05 -04:00
|
|
|
const a = new RenderedTarget(s, r);
|
2017-04-07 14:50:13 -04:00
|
|
|
a.renderer = renderer;
|
|
|
|
a.goToFront();
|
|
|
|
t.equals(a.renderer.order, 5);
|
2017-12-28 14:52:06 -05:00
|
|
|
a.goBackwardLayers(2);
|
2017-04-07 14:50:13 -04:00
|
|
|
t.equals(a.renderer.order, 3);
|
2017-12-28 14:52:06 -05:00
|
|
|
a.goToBack();
|
|
|
|
t.equals(a.renderer.order, 1);
|
|
|
|
a.goForwardLayers(1);
|
|
|
|
t.equals(a.renderer.order, 2);
|
2017-04-07 14:50:13 -04:00
|
|
|
o.drawableID = 999;
|
|
|
|
a.goBehindOther(o);
|
|
|
|
t.equals(a.renderer.order, 1);
|
|
|
|
t.end();
|
|
|
|
});
|
|
|
|
|
2017-04-20 19:17:05 -04:00
|
|
|
test('keepInFence', t => {
|
|
|
|
const s = new Sprite();
|
|
|
|
const r = new Runtime();
|
|
|
|
const renderer = new FakeRenderer();
|
2017-04-07 14:50:13 -04:00
|
|
|
r.attachRenderer(renderer);
|
2017-04-20 19:17:05 -04:00
|
|
|
const a = new RenderedTarget(s, r);
|
2017-04-07 14:50:13 -04:00
|
|
|
a.renderer = renderer;
|
|
|
|
t.equals(a.keepInFence(1000, 1000)[0], 240);
|
|
|
|
t.equals(a.keepInFence(-1000, 1000)[0], -240);
|
|
|
|
t.equals(a.keepInFence(1000, 1000)[1], 180);
|
|
|
|
t.equals(a.keepInFence(1000, -1000)[1], -180);
|
|
|
|
t.end();
|
|
|
|
});
|
|
|
|
|
2017-04-20 19:17:05 -04:00
|
|
|
test('#stopAll clears graphics effects', t => {
|
|
|
|
const s = new Sprite();
|
|
|
|
const r = new Runtime();
|
|
|
|
const a = new RenderedTarget(s, r);
|
|
|
|
const effectName = 'brightness';
|
2017-03-06 16:15:04 -05:00
|
|
|
a.setEffect(effectName, 100);
|
|
|
|
a.onStopAll();
|
|
|
|
t.equals(a.effects[effectName], 0);
|
|
|
|
t.end();
|
|
|
|
});
|
2017-04-06 09:46:40 -04:00
|
|
|
|
2017-04-20 19:17:05 -04:00
|
|
|
test('#getCostumes returns the costumes', t => {
|
|
|
|
const spr = new Sprite();
|
|
|
|
const a = new RenderedTarget(spr, null);
|
|
|
|
const costumes = [1, 2, 3];
|
2017-04-06 09:46:40 -04:00
|
|
|
a.sprite.costumes = costumes;
|
|
|
|
t.equals(a.getCostumes(), costumes);
|
|
|
|
t.end();
|
|
|
|
});
|
|
|
|
|
2017-04-20 19:17:05 -04:00
|
|
|
test('#getSounds returns the sounds', t => {
|
|
|
|
const spr = new Sprite();
|
|
|
|
const a = new RenderedTarget(spr, null);
|
|
|
|
const sounds = [1, 2, 3];
|
2017-04-06 09:46:40 -04:00
|
|
|
a.sprite.sounds = sounds;
|
|
|
|
t.equals(a.getSounds(), sounds);
|
|
|
|
t.end();
|
|
|
|
});
|
|
|
|
|
2017-04-20 19:17:05 -04:00
|
|
|
test('#toJSON returns the sounds and costumes', t => {
|
|
|
|
const spr = new Sprite();
|
|
|
|
const a = new RenderedTarget(spr, null);
|
|
|
|
const sounds = [1, 2, 3];
|
|
|
|
const costumes = ['a', 'b', 'c'];
|
2017-04-06 09:46:40 -04:00
|
|
|
a.sprite.sounds = sounds;
|
|
|
|
a.sprite.costumes = costumes;
|
|
|
|
t.same(a.toJSON().sounds, sounds);
|
|
|
|
t.same(a.toJSON().costumes, costumes);
|
|
|
|
t.end();
|
|
|
|
});
|
2017-07-25 10:32:25 -04:00
|
|
|
|
|
|
|
test('#addSound does not duplicate names', t => {
|
|
|
|
const spr = new Sprite();
|
|
|
|
const a = new RenderedTarget(spr, null);
|
|
|
|
a.sprite.sounds = [{name: 'first'}];
|
|
|
|
a.addSound({name: 'first'});
|
|
|
|
t.deepEqual(a.sprite.sounds, [{name: 'first'}, {name: 'first2'}]);
|
|
|
|
t.end();
|
|
|
|
});
|
|
|
|
|
|
|
|
test('#addCostume does not duplicate names', t => {
|
|
|
|
const spr = new Sprite();
|
|
|
|
const a = new RenderedTarget(spr, null);
|
|
|
|
a.sprite.costumes = [{name: 'first'}];
|
|
|
|
a.addCostume({name: 'first'});
|
|
|
|
t.deepEqual(a.sprite.costumes, [{name: 'first'}, {name: 'first2'}]);
|
|
|
|
t.end();
|
|
|
|
});
|
|
|
|
|
|
|
|
test('#renameSound does not duplicate names', t => {
|
|
|
|
const spr = new Sprite();
|
|
|
|
const a = new RenderedTarget(spr, null);
|
|
|
|
a.sprite.sounds = [{name: 'first'}, {name: 'second'}];
|
|
|
|
a.renameSound(0, 'first'); // Shouldn't increment the name, noop
|
|
|
|
t.deepEqual(a.sprite.sounds, [{name: 'first'}, {name: 'second'}]);
|
|
|
|
a.renameSound(1, 'first');
|
|
|
|
t.deepEqual(a.sprite.sounds, [{name: 'first'}, {name: 'first2'}]);
|
|
|
|
t.end();
|
|
|
|
});
|
|
|
|
|
|
|
|
test('#renameCostume does not duplicate names', t => {
|
|
|
|
const spr = new Sprite();
|
|
|
|
const a = new RenderedTarget(spr, null);
|
|
|
|
a.sprite.costumes = [{name: 'first'}, {name: 'second'}];
|
|
|
|
a.renameCostume(0, 'first'); // Shouldn't increment the name, noop
|
|
|
|
t.deepEqual(a.sprite.costumes, [{name: 'first'}, {name: 'second'}]);
|
|
|
|
a.renameCostume(1, 'first');
|
|
|
|
t.deepEqual(a.sprite.costumes, [{name: 'first'}, {name: 'first2'}]);
|
|
|
|
t.end();
|
|
|
|
});
|