Lively Kernel canvas
getRandomNumber = function(max) { return Math.floor(Math.random()*max+1)-1};
getRandomPoint = function(maxX, maxY) { return pt(getRandomNumber(maxX), getRandomNumber(maxY)) };
getRandomPtFromRect = function(r) { return getRandomPoint(r.width, r.height).addPt(r.topLeft())}
BoxMorph.subclass('Mouse', {
howFarCanIGoWithOneStep: 5,
maxTurn: Math.PI/2,
initialize: function($super) {
$super(new Rectangle(45, 45, 49, 49));
var makeStarVertices = function(r,center,startAngle) {
var vertices = [];
var nVerts = 10;
for (var i=0; i <= nVerts; i++) {
var a = startAngle + (2*Math.PI/nVerts*i);
var p = Point.polar(r,a);
if (i%2 == 0) p = p.scaleBy(0.39);
vertices.push(p.addPt(center));
}
return vertices;
}
var widget = Morph.makePolygon(makeStarVertices(50,pt(0,0),0), 1, Color.black, Color.yellow);
this.setShape(widget.shape);
// move position to the center of the morph instead of top left corner, nicer rotating behavior
//this.moveOriginBy(this.getExtent().scaleBy(0.5));
// remove the blue background
//this.setFill(null);
},
calculateNewPosition: function() {
var p = this.getPosition();
var angle = this.getRotation()+Math.PI*(1/2);
var r = this.howFarCanIGoWithOneStep;
return pt(p.x+r*Math.cos(angle), p.y+r*Math.sin(angle));
},
moveOneStep: function() {
this.mayBeChangeDirection();
var newPos = this.calculateNewPosition();
// while (!WorldMorph.current().bounds().containsRect(this.bounds().translatedBy(newPos))) {
var activeBounds = pt(document.body.clientWidth, document.body.clientHeight).subPt(this.getExtent()).extentAsRectangle();
var i=0;
while (!activeBounds.containsPoint(newPos)) {
this.newDirection();
newPos = this.calculateNewPosition();
i++;
if (i>100) newPos = pt(100,100);
}
this.setPosition(newPos);
this.eatCheese();
},
mayBeChangeDirection: function() { if (getRandomNumber(10) == 5) this.newDirection() },
newDirection: function() {
this.rotateBy(getRandomNumber(this.maxTurn*100)/100 - this.maxTurn/2);
},
startSteppingScripts: function() { this.startStepping(20, 'moveOneStep') },
eatCheese: function() {
this.owner.submorphs
.select(function(ea) { return ea.isCheese && this.bounds().intersect(ea.bounds()) }, this)
.forEach(function(ea) { ea.remove(); console.log('Hmmm, delicious!') });
}
});
null
20
177.1730361707469
0.3075342616644142
null
null
20
185.1848840915673
0.330451614549585
null
20
129.51583247235203
null
0.8166461047419331
null
20
220.68822155289996
null
1.883460797562347
null
20
164.65430727444047
null
-1.6489281828223472
20
191.59863766625284
1.2844332485240697
null
20
162.24896161730598
null
-0.343706143591727
null
20
190.11047160325018
-1.9651226730146918
null
20
166.48348360445848
null
-0.5985759344886916
null
20
215.26103332848257
null
1.266998353075587
null
20
142.42917503841517
null
-3.3531777710912447
null
20
168.0020764471432
null
-0.32574287564276017
null
20
116.65171115677242
null
-1.2115851175014463
null
20
152.59371962015342
null
0.06310854922869302
20
106.90446491459417
null
-1.0653906273091032
null
20
174.1112630608
null
-2.233001646924412
20
154.14853047999998
null
-1.020788790706554
20
125.73413329000003
null
-1.1455667514759296
20
144.20515259
-2.302205320129519
null
1234421620323
26904
1
11
null
1234421620
null