expect(function () { Grape.Class({ 'global-event x': function () { } }); }).toThrow();
it('global-event keyword', function () { expect(function () { Grape.Class({ 'global-event x': function () { } }); }).toThrow(); var X = Grape.Class(Grape.GameObject, { init: function () { this.count = 0; }, 'global-event x': function (ev) { this.count++; this.ev = ev; } }); var x = new X(); var layer = new Grape.Layer(); layer.add(x); layer.emit('x', 123); expect(x.count).toBe(1); expect(x.ev).toBe(123); x.remove(); layer.emit('x', 123); expect(x.count).toBe(1); });
it('should throw error when the instance added is not a GameObject', function () { var layer = new Grape.Layer(); var NotGameObject = Grape.Class(); expect(function () { layer.add({}); }).toThrow(); expect(function () { layer.add(new NotGameObject()); }).toThrow(); });
it('should work with finals', function () { var B = Grape.Class({ 'final chainable setX': function (x) { this.x = x; } }); //if we extend from a class with a final method proxy, we should check overriding against the proxied method. B.extend(); expect(function () { Grape.Class(B, { setX: function () { } }); }).toThrow(); });
describe('chainable test', function () { var Grape = require('grape'); var A = Grape.Class({ 'chainable setX': function (x) { this.x = x; } }); it('should work same way as the original method', function () { var a = new A(); a.setX(10); expect(a.x).toBe(10); }); it('should be chained by returning this', function () { var a = new A(); expect(a.setX(10)).toBe(a); a.setX(20).setX(30); expect(a.x).toBe(30); }); it('should work with finals', function () { var B = Grape.Class({ 'final chainable setX': function (x) { this.x = x; } }); //if we extend from a class with a final method proxy, we should check overriding against the proxied method. B.extend(); expect(function () { Grape.Class(B, { setX: function () { } }); }).toThrow(); }); });
describe('animation test', function () { var Grape = require('grape'); var PRECISION = 4; var A = Grape.Class(Grape.Animation, { init: function () { this.x = 0; this.imageSpeed = 0.4; this.sprite = { subimages: 3 }; }, 'event animationEnd': function () { this.x++; } }); it('subimage changing', function () { var a = new A(); var layer = new Grape.Layer(); layer.add(a); expect(a.subimage).toBe(0); layer.emit('frame'); expect(a.subimage).toBeCloseTo(0.4, PRECISION); layer.emit('frame'); expect(a.subimage).toBeCloseTo(0.8, PRECISION); }); it('animationEnd event', function () { var a = new A(); var layer = new Grape.Layer(); layer.add(a); a.subimage = 2.8; expect(a.x).toBe(0); layer.emit('frame'); expect(a.x).toBe(1); expect(a.subimage).toBeCloseTo(0.2, PRECISION); }); it('negative speed', function () { var a = new A(); var layer = new Grape.Layer(); layer.add(a); a.imageSpeed = -0.4; expect(a.x).toBe(0); layer.emit('frame'); expect(a.subimage).toBeCloseTo(2.6, PRECISION); expect(a.x).toBe(1); }); it('round speed', function () { var a = new A(); var layer = new Grape.Layer(); layer.add(a); a.imageSpeed = -1; expect(a.x).toBe(0); layer.emit('frame'); expect(a.subimage).toBe(2); expect(a.x).toBe(1); a.imageSpeed = 1; layer.emit('frame'); expect(a.subimage).toBe(0); expect(a.x).toBe(2); }); it('speed>subimages', function () { //TODO }); it('no sprite', function () { var a = new Grape.Animation(); //no sprite var layer = new Grape.Layer(); layer.add(a); layer.emit('frame'); }); });
expect(function () { Grape.Class({ 'collision X': function () { } }); }).toThrow();
describe('collision test', function () { var Grape = require('grape'); var A = Grape.Class([Grape.Collidable, Grape.Rectangle], { init: function () { this.counter = 0; }, 'collision B': function () { this.counter++; }, 'collision C': function () { } }); var B = Grape.Class([Grape.Collidable, Grape.Rectangle], { 'event add': function () { this.addTag('B'); } }); it('should not work without the Collision class', function () { expect(function () { Grape.Class({ 'collision X': function () { } }); }).toThrow(); }); it('simple collision', function () { var a = new A({x: 10, y: 10, width: 10, height: 10}); var b = new B({x: 10, y: 15, width: 10, height: 10}); var layer = new Grape.Layer(); layer.add(a); layer.add(b); layer.addSystem(new Grape.CollisionSystem()); expect(a.counter).toBe(0); layer.emit('frame'); expect(a.counter).toBe(1); }); it('collision set in parents', function () { var C = A.extend({ init: function () { this.counter2 = 0; }, 'collision B': function () { this.counter2++; } }); var c = new C({x: 10, y: 10, width: 10, height: 10}); var b = new B({x: 10, y: 15, width: 10, height: 10}); var layer = new Grape.Layer(); layer.add(c); layer.add(b); layer.addSystem(new Grape.CollisionSystem()); expect(c.counter).toBe(0); expect(c.counter2).toBe(0); layer.emit('frame'); expect(c.counter).toBe(1); expect(c.counter2).toBe(1); }); it('multiple instances in the same grid', function () { var a1 = new A({x: 10, y: 10, width: 10, height: 10}); var a2 = new A({x: 15, y: 10, width: 10, height: 10}); var b1 = new B({x: 10, y: 15, width: 10, height: 10}); var b2 = new B({x: 10, y: 10, width: 10, height: 125}); //grid overlapping var layer = new Grape.Layer(); layer.add(a1); layer.add(a2); layer.add(b1); layer.add(b2); layer.addSystem(new Grape.CollisionSystem()); layer.emit('frame'); expect(a1.counter).toBe(2); expect(a2.counter).toBe(2); }); it('corners', function () { var left = new A({x: 0, y: 10, width: 10, height: 10}); var top = new A({x: 10, y: 0, width: 10, height: 10}); var right = new A({x: 20, y: 10, width: 10, height: 10}); var bottom = new A({x: 10, y: 20, width: 10, height: 10}); var center = new A({x: 10, y: 10, width: 10, height: 10}); var target = new B({x: 10, y: 10, width: 10, height: 10}); var layer = new Grape.Layer(); layer.add(left); layer.add(top); layer.add(right); layer.add(bottom); layer.add(center); layer.add(target); layer.addSystem(new Grape.CollisionSystem()); layer.emit('frame'); expect(left.counter).toBe(0); expect(top.counter).toBe(0); expect(right.counter).toBe(0); expect(bottom.counter).toBe(0); expect(center.counter).toBe(1); }); it('static partition', function () { var a = new A({x: 10, y: 10, width: 10, height: 10}); var b = new B({x: 10, y: 15, width: 10, height: 10}); var layer = new Grape.Layer(); var cs = new Grape.CollisionSystem(); layer.addSystem(cs); cs.createStaticPartition(A); cs.createStaticPartition('B'); //no instance added when partitions created layer.add(a); layer.add(b); layer.emit('frame'); expect(a.counter).toBe(0); cs.createStaticPartition(A); layer.emit('frame'); expect(a.counter).toBe(0); cs.removeStaticPartition('B'); layer.emit('frame'); expect(a.counter).toBe(1); a.x += 10; layer.emit('frame'); expect(a.counter).toBe(2); cs.removeStaticPartition(A); layer.emit('frame'); expect(a.counter).toBe(2); }); it('same instance, double emission', function () { var a = new A({x: 60, y: 60, width: 10, height: 10}); //place at grid corner var b = new B({x: 60, y: 60, width: 10, height: 10}); var layer = new Grape.Layer(); layer.add(a); layer.add(b); a.addTag('B'); //check avoid self collision layer.addSystem(new Grape.CollisionSystem()); layer.emit('frame'); expect(a.counter).toBe(1); // }); });
expect(function () { Grape.Class({ 'event a': function () { } }); }).toThrow();
expect(function () { Grape.Class(B, { setX: function () { } }); }).toThrow();