Path: blob/main/public/games/files/garbage-collector/js/object2d.js
1036 views
/*globals define*/1define([2'base-object',3'color'4], function( BaseObject, Color ) {5'use strict';67function Object2D( x, y ) {8BaseObject.call( this );910this.x = x || 0;11this.y = y || 0;1213this.angle = 0;1415this.fill = new Color();16this.stroke = new Color();1718this.lineWidth = 0;19}2021Object2D.prototype = new BaseObject();22Object2D.prototype.constructor = Object2D;2324Object2D.prototype.update = function() {};25Object2D.prototype.drawPath = function() {};2627Object2D.prototype.draw = function( ctx ) {28ctx.save();2930ctx.translate( this.x, this.y );31ctx.rotate( -this.angle );3233this.drawPath( ctx );3435ctx.restore();3637if ( this.fill.alpha ) {38ctx.fillStyle = this.fill.rgba();39ctx.fill();40}4142if ( this.lineWidth && this.stroke.alpha ) {43ctx.lineWidth = this.lineWidth;44ctx.strokeStyle = this.stroke.rgba();45ctx.stroke();46}47};4849Object2D.prototype.toWorld = function( x, y ) {50var cos, sin;51var rx, ry;5253if ( this.angle ) {54cos = Math.cos( -this.angle );55sin = Math.sin( -this.angle );5657rx = cos * x - sin * y;58ry = sin * x + cos * y;5960x = rx;61y = ry;62}6364return {65x: x + this.x,66y: y + this.y67};68};6970Object2D.prototype.toLocal = function( x, y ) {71x -= this.x;72y -= this.y;7374var cos, sin;75var rx, ry;7677if ( this.angle ) {78cos = Math.cos( this.angle );79sin = Math.sin( this.angle );8081rx = cos * x - sin * y;82ry = sin * x + cos * y;8384x = rx;85y = ry;86}8788return {89x: x,90y: y91};92};9394Object2D.prototype.aabb = function() {95return {96xmin: this.x,97ymin: this.y,98xmax: this.x,99ymax: this.y100};101};102103return Object2D;104});105106107