Язык программирования:
Javascript
Среда программирования:
WebStorm, Phaser.js
function Start() { var game = new Phaser.Game(720, 720, Phaser.AUTO, "", { create: Create, update: Update, render: Render }); var sun; var bmd; function Create() { //Инициализируем BitMapData для рисования траекторий bmd = game.add.bitmapData(game.width, game.height); bmd.addToWorld(); //Создаем солнце sun = new Body(64, "#FFD700", 0, 0); //Создаем планеты sun.AddSatellite(new Body(32, "#808000", 125, 1)); sun.AddSatellite(new Body(48, "#4682B4", 210, 1.5)); sun.AddSatellite(new Body(24, "#4B0082", 300, 2)); //Создаем спутники планет sun.satellites[0].AddSatellite(new Body(8, "#666666", 40, 3)); sun.satellites[1].AddSatellite(new Body(12, "#AAAAAA", 40, 4)); sun.satellites[1].AddSatellite(new Body(8, "#CCCCCC", 50, 6)); //Рисуем траектории планет sun.satellites.forEach(function (item, i, arr) { item.DrawTrail(bmd); }); } function Update() { //вращаем все спутники солнца sun.RotateSatellites(); } function Render() { //Рисуем солнце и его спутники sun.Draw(); sun.DrawSatellites(); } //конструктор тел function Body(radius, color, distance, RPS) { this.rotatonPerSecond = RPS; //скорость вращения в радианах this.color = color; //цвет тела this.distance = distance; //отступ от родителя this.rotation = Math.PI; //текущий угол вращения в радианах this.satellites = []; //массив спутников this.parent = undefined; //ссылка на родителя var pos = {x: game.world.centerX, y: game.world.centerY}; this.position = pos; //текущая позиция this.circle = new Phaser.Circle(this.position.x, this.position.y, radius); //геометрическое тело //метод для добавления спутника к существующему телу this.AddSatellite = function (sat) { sat.parent = this; sat.position.x = this.position.x; sat.position.y = this.position.y; this.satellites[this.satellites.length] = sat; }; //метод для вращения всех спутников (с рекурсивным спуском) this.RotateSatellites = function () { this.satellites.forEach(function (item, i, arr) { item.Rotate(); item.RotateSatellites(); }); } //метод для вращения текущего тела this.Rotate = function () { //если есть родитель, принять его позицию за центр отсчета if(this.parent !== undefined) { this.position.x = this.parent.circle.x; this.position.y = this.parent.circle.y; } //прибавляем к текущему углу скорость вращения this.rotation += this.rotatonPerSecond / 60; //вычисляем новую позицию геометрического тела this.circle.x = this.position.x + this.distance * Math.cos(this.rotation); this.circle.y = this.position.y + this.distance * Math.sin(this.rotation); }; //метод для отрисовки всех спутников (с рекурсивным спуском) this.DrawSatellites = function () { this.satellites.forEach(function (item, i, arr) { item.Draw(); item.DrawSatellites(); }); } //метод для отрисовки текущего тела this.Draw = function () { game.debug.geom(this.circle, this.color); }; //метод для отрисовки траектории this.DrawTrail = function (bmd) { //от 0 до 2пи с промежутком в 10 пикселей for (var rot = 0; rot < Math.PI * 2; rot += 10 / this.distance) { //создаем точку с углом вращения rot на расстоянии отступа текущего тела от своего родителя var p = new Phaser.Point(this.position.x + this.distance * Math.cos(rot), this.position.y + this.distance * Math.sin(rot)); //bmd.setPixel должен принимать целочисленные значения, поэтому координаты точки мы округляем p.floor(); //рисуем в битмапе текущую точку с цветом r=100 g=100 b=100 bmd.setPixel(p.x, p.y, 100, 100, 100); } } } }
Прикрепленный файл | Размер |
---|---|
gaponov_demo.rar | 165.29 кб |