var Class = require('../../utils/Class'); var GameObject = require('../GameObject'); var Components = require('../../components'); var Render = require('./GraphicsRender'); var Commands = require('./Commands'); var MATH_CONST = require('../../math/const'); var Graphics = new Class({ Mixins: [ Components.Alpha, Components.BlendMode, Components.Transform, Components.Visible, Render ], initialize: function Graphics (state, x, y) { GameObject.call(this, state); this.setPosition(x, y); this.commandBuffer = []; }, arc: function (x, y, radius, startAngle, endAngle, anticlockwise) { this.commandBuffer.push( Commands.ARC, x, y, radius, startAngle, endAngle, anticlockwise ); }, lineStyle: function (lineWidth, color, alpha) { this.commandBuffer.push( Commands.LINE_STYLE, lineWidth, color, alpha ); }, fillStyle: function (color, alpha) { if (alpha === undefined) { alpha = 1; } this.commandBuffer.push( Commands.FILL_STYLE, color, alpha ); }, beginPath: function () { this.commandBuffer.push( Commands.BEGIN_PATH ); }, closePath: function () { this.commandBuffer.push( Commands.CLOSE_PATH ); }, fillPath: function () { this.commandBuffer.push( Commands.FILL_PATH ); }, strokePath: function () { this.commandBuffer.push( Commands.STROKE_PATH ); }, fillCircle: function (x, y, radius) { this.beginPath(); this.arc(x, y, radius, 0, MATH_CONST.PI2); this.fillPath(); this.closePath(); }, fillRect: function (x, y, width, height) { this.commandBuffer.push( Commands.FILL_RECT, x, y, width, height ); }, fillTriangle: function (x0, y0, x1, y1, x2, y2) { this.commandBuffer.push( Commands.FILL_TRIANGLE, x0, y0, x1, y1, x2, y2 ); }, strokeCircle: function (x, y, radius) { this.beginPath(); this.arc(x, y, radius, 0, MATH_CONST.PI2); this.closePath(); this.strokePath(); }, strokeRect: function (x, y, width, height) { this.beginPath(); this.moveTo(x, y); this.lineTo(x + width, y); this.lineTo(x + width, y + height); this.lineTo(x, y + height); this.lineTo(x, y); this.strokePath(); this.closePath(); }, strokeTriangle: function (x0, y0, x1, y1, x2, y2) { this.commandBuffer.push( Commands.STROKE_TRIANGLE, x0, y0, x1, y1, x2, y2 ); }, lineTo: function (x, y) { this.commandBuffer.push( Commands.LINE_TO, x, y ); }, moveTo: function (x, y) { this.commandBuffer.push( Commands.MOVE_TO, x, y ); }, lineFxTo: function (x, y, width, rgb) { this.commandBuffer.push( Commands.LINE_FX_TO, x, y, width, rgb, 1 ); }, moveFxTo: function (x, y, width, rgb) { this.commandBuffer.push( Commands.MOVE_FX_TO, x, y, width, rgb, 1 ); }, clear: function () { this.commandBuffer.length = 0; } }); module.exports = Graphics;