Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- "use strict";
- function initialize(theCanvasID) {
- theSimulation = new World(theCanvasID);
- /** >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> */
- var kuchen;
- theSimulation.addNewGroup("Kuchen", true, true);
- var i;
- for (i = 0; i < 10; i++) {
- kuchen = new Kuchen(theSimulation, "Kuchen" + i);
- kuchen.setPosRandom(50, 200, 500, 200);
- theSimulation.addObjectToGroup(kuchen, "Kuchen");
- }
- var smiley;
- theSimulation.addNewGroup("Smiley", true, true);
- {
- var i;
- for (i = 0; i < 4; i++) {
- smiley = new Smiley(theSimulation, "Smiley" + i);
- smiley.setPosRandom(100, 200, 500, 200);
- smiley.setVelocityRandom(0, 1, 0, 0);
- smiley.setRotationVelocityRandom(0, 0);
- theSimulation.addObjectToGroup(smiley, "Smiley");
- }
- }
- /** <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<< */
- theSimulation.animate();
- }
- class World extends Simulation {
- constructor(theCanvasID) {
- //Aufruf der Superklasse
- super(theCanvasID);
- }
- //die Funktion drawBackground muss hier überschrieben werden.
- drawBackground() {
- this.ctx.save();
- /** *>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>** */
- this.ctx.fillStyle = "rgba(255,255,0,1.0)";
- this.ctx.fillRect(0, 0, this.canvas.width, this.canvas.height);
- this.ctx.fillStyle = "rgba(255,125,0,1.0)";
- this.ctx.fillRect(0, 200, this.canvas.width, this.canvas.height);
- this.ctx.fillStyle = "rgba(0,255,0,1.0)";
- this.ctx.fillRect(0, 275, this.canvas.width, this.canvas.height);
- this.ctx.fillStyle = "rgba(255,15,0,1.0)";
- this.ctx.fillRect(0, 375, this.canvas.width, this.canvas.height);
- /** *>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>** */
- this.ctx.restore();
- }
- }
- /** ******************************************************************** */
- /** ********** Objekte ************************************************** */
- /** ******************************************************************** */
- class DemoBox extends SimObject {
- constructor(theSimulation, objName) {
- //Aufruf der Superklasse
- super(theSimulation, objName);
- }
- //die Funktion draw muss hier überschrieben werden.
- draw() {
- this.simInstance.ctx.save();
- // Transformation
- this.simInstance.ctx.translate(this.xPos, this.yPos);
- this.simInstance.ctx.rotate(this.rotation * Math.PI / 180);
- this.simInstance.ctx.scale(this.xScale, this.yScale);
- /** *<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<** */
- // Farben festlegen
- this.simInstance.ctx.fillStyle = "rgba(255,0,0,1.0)";
- // Objekt zeichnen
- this.simInstance.ctx.fillRect(0, 0, 50, 50);
- /** *>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>** */
- this.simInstance.ctx.restore();
- }
- }
- class Kuchen extends SimObject {
- img;
- constructor(theSimulation, objName) {
- super(theSimulation, objName);
- this.img = new Image();
- this.img.src = '../img/canvas-img/kuchen.jpg';
- }
- //die Funktion draw muss hier überschrieben werden.
- draw() {
- this.simInstance.ctx.save();
- // Transformation
- this.simInstance.ctx.translate(this.xPos, this.yPos);
- this.simInstance.ctx.rotate(this.rotation * Math.PI / 180);
- this.simInstance.ctx.scale(this.xScale, this.yScale);
- /** *<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<** */
- // Farben festlegen
- this.simInstance.ctx.drawImage(this.img, -35, -35);
- /** *>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>** */
- this.simInstance.ctx.restore();
- }
- }
- class Smiley extends SimObject {
- img;
- imgArray = ['../img/canvas-img/smiley.png', '../img/canvas-img/smily2.png', '../images/smily3.png'];
- actImgIndex = 0;
- constructor(theSimulation, objName) {
- super(theSimulation, objName);
- var rnd = Math.random()*5+1;
- this.img = new Image();
- this.img.src = 'imgArray[rnd]';
- }
- calcNextStep() {
- var theCanvasBox = theSimulation.getCanvasRect();
- /** *<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<** */
- if ((this.xPos >= theCanvasBox.width) || (this.xPos <= 0)) {
- //this.xPos = theCanvasBox.width;
- this.xVel = this.xVel * (-1);
- }
- if ((this.yPos >= theCanvasBox.height) || (this.yPos <= 0)) {
- //this.yPos = theCanvasBox.height;
- this.yVel = this.yVel * (-1);
- }
- this.xVel = this.xVel + this.xAccel;
- this.yVel = this.yVel + this.yAccel;
- this.rotationVel = this.rotationVel + this.rotationAccel;
- this.xScaleVel = this.xScaleVel + this.xScaleAccel;
- this.yScaleVel = this.yScaleVel + this.yScaleAccel;
- this.xPos = this.xPos + this.xVel;
- this.yPos = this.yPos + this.yVel;
- this.rotation = this.rotation + this.rotationVel;
- this.xScale = this.xScale + this.xScaleVel;
- this.yScale = this.yScale + this.yScaleVel;
- /** *>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>** */
- this.syncColBody();
- }
- //die Funktion draw muss hier überschrieben werden.
- draw() {
- this.simInstance.ctx.save();
- // Transformation
- this.simInstance.ctx.translate(this.xPos, this.yPos);
- this.simInstance.ctx.rotate(this.rotation * Math.PI / 180);
- this.simInstance.ctx.scale(this.xScale, this.yScale);
- /** *<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<** */
- this.simInstance.ctx.drawImage(this.img, -35, -35);
- /** *>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>** */
- this.simInstance.ctx.restore();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement