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
286.5644418123595
-0.3492441477817312
null
null
20
265.0791219972404
-1.5401685880784828
null
null
20
262.7342653020597
3.1473567404301424
null
null
20
201.28598897892
-0.48697714187799657
null
20
293.4873590578744
1.6181681394017275
null
20
242.1784077878454
-1.7813802460486996
null
null
20
464.604772737353
-3.139364725195242
null
null
20
228.95405954068664
2.7896764989522618
null
20
267.39693767676135
3.0215295707481418
null
null
20
232.99288299094925
-1.1755304681016616
null
null
20
301.82112057163147
3.70835933574525
null
20
232.2798487910394
1.307111394400757
null
null
20
265.3395496011047
1.8279995513232525
null
null
20
203.66323039055078
1.4923965164730342
null
20
224.4311872132713
-2.3485759344886934
null
null
20
292.6011132791842
1.5109169042084187
null
null
20
219.1496230698681
-2.8278502266237986
null
null
20
222.932509461616
-0.12236776918711023
null
null
20
253.84361286631432
-0.03801881470226953
null
null
thiswouldhavemademoresenseifihadbeenabletoupdatetheoriginal:-)
19
true
null
18
null
null
1234951588236
1426
1
14
null
1234951588
null