forked from: Heliocentric
import planets.Planet;
// forked from medavox's Heliocentric
package
{
import flash.display.MovieClip;
import flash.display.Sprite;
import flash.events.Event;
import flash.events.MouseEvent;
import flash.geom.Point;
//import planets.Planet;
[SWF(width="600", height="600", backgroundColor="0", frameRate="30")]
public class HelioCentric extends MovieClip
{
private var earth:Planet = new Planet();
private var sun:Planet = new Planet();
private var mercury:Planet = new Planet();
private var moon:Planet = new Planet();
private var venus:Planet = new Planet();
private var mars:Planet = new Planet();
private var currentScale:Number = 0.7;
private var solSys:Sprite = new Sprite();
private var trailPlanets:Array = new Array();
private var trails:Sprite = new Sprite();
private var oldPoints:Array;
public function HelioCentric()
{
stage.addEventListener(MouseEvent.MOUSE_WHEEL, wheely);
sun.drawPlanet(0xffff00, 50, 0xffffff);
sun.setLabel("Sun", 0x0);
solSys.addChild(sun);
earth.trailColour = 0x0000ff;
earth.showTrails = true;
earth.drawPlanet(0x0000ff);
earth.setOrbit(sun, sun, 280, 3650);
earth.drawShadow();
earth.setLabel("Earth");
solSys.addChild(earth);
trailPlanets.push(earth);
mercury.drawPlanet(0xeecc44, 20);
mercury.drawShadow();
mercury.setLabel("Mercury");
mercury.setOrbit(sun, sun, 110, 88);
solSys.addChild(mercury);
trailPlanets.push(mercury);
venus.drawPlanet(0xffccff);
venus.setOrbit(sun, sun, 170, 245);
venus.drawShadow();
venus.setLabel("Venus");
solSys.addChild(venus);
trailPlanets.push(venus);
mars.showTrails = true;
mars.trailColour = 0xff0000;
mars.drawPlanet(0xffaa00, 22);
mars.drawShadow();
mars.setLabel("Mars");
mars.setOrbit(sun, sun, 400, 686);
solSys.addChild(mars);
trailPlanets.push(mars);
moon.showTrails = true;
moon.drawPlanet(0xcccccc, 14);
moon.drawShadow();
moon.setLabel("The Moon");
moon.setOrbit(earth, sun, 50, 28);
solSys.addChild(moon);
trailPlanets.push(moon);
solSys.x = stage.stageWidth / 2;
solSys.y = stage.stageHeight / 2;
//trails.x = stage.stageHeight / 2;
//trails.y = stage.stageWidth / 2;
solSys.addChild(trails);
solSys.scaleX = currentScale;
solSys.scaleY = currentScale;
addChild(solSys);
oldPoints = new Array(trailPlanets.length);
for(var i:uint = 0; i < oldPoints.length; i++)
{
oldPoints[i] = new Point(trailPlanets[i].x, trailPlanets[i].y);
}
addEventListener(Event.ENTER_FRAME, loop);
}
public function wheely(m:MouseEvent):void
{
if (m.delta < 0)
{
//wheel down:
trace("wheel down");
var oldmx:Number = mouseX;
var oldmy:Number = mouseY;
if(currentScale > 0)
{currentScale -= 0.1;}
else{currentScale = 0;}
trace("currentScale: "+currentScale);
solSys.scaleX = currentScale;
solSys.scaleY = currentScale;
}
else if (m.delta > 0)
{
//wheel up:
trace("wheel up");
var oldmx:Number = mouseX;
var oldmy:Number = mouseY;
currentScale += 0.1;
trace("currentScale: "+currentScale);
solSys.scaleX = currentScale;
solSys.scaleY = currentScale;
}
}
public function loop(e:Event):void
{
for(var i:uint=0; i < trailPlanets.length; i++)
{
trailPlanets[i].orbit();
trailPlanets[i].updateShadow();
if(trailPlanets[i].showTrails)
{
trails.graphics.lineStyle(0, trailPlanets[i].trailColour);
trails.graphics.moveTo(oldPoints[i].x, oldPoints[i].y);
trails.graphics.lineTo(trailPlanets[i].x, trailPlanets[i].y);
oldPoints[i].x = trailPlanets[i].x;
oldPoints[i].y = trailPlanets[i].y;
}
}
}
}
}
import flash.display.Sprite;
import flash.display.Shape;
import flash.events.Event;
import flash.text.TextField;
import flash.text.TextFormat;
class Planet extends Sprite
{
public var radius:uint = 50;
private var fokus:Planet;
public var angularSpeedRad:Number;
private var angleRad:Number = 0;
private var sun:Planet;
private var nabel:String;
private var tf:TextFormat;
private var t:TextField;
private var shadow:Shape = new Shape();
public var showTrails:Boolean = false;
public var trailColour:uint = 0xffffff;
//private var trail:Sprite = new Sprite();
public function Planet():void
{}
public function setLightSource(soun:Planet)
{
sun = soun;
}
public function drawPlanet(colour:uint, radius:uint=30, lineColour:uint=0):void
{
graphics.clear();
graphics.lineStyle(0, lineColour);
graphics.beginFill(colour);
graphics.drawCircle(0, 0, radius);
}
public function setOrbit
(fokus:Planet, sun:Planet, radius:uint = 0, period:Number = 0):void
{
this.fokus = fokus;
this.sun = sun;
if(radius == 0)
{}
else{this.radius = radius;}
//trace("period: "+period);
angularSpeedRad = 2*Math.PI/period;
//trace("angularSpeedRad on set: "+angularSpeedRad);
}
public function setLabel(nabel:String, textColour:uint=0xffffff, size:uint = 20):void
{
this.nabel = nabel;
tf = new TextFormat("Arial", size, textColour);
t = new TextField();
t.defaultTextFormat = tf;
t.selectable = false;
t.autoSize = "left";
t.text = nabel;
t.textColor = textColour;
addChild(t);
}
public function drawShadow():void
{
//graphics.clear();
var colours:Array = new Array(0xffffff, 0x000000);
var alphas:Array = new Array(0, 0.9);
var ratios:Array = new Array(0, 140);
shadow.graphics.beginGradientFill("linear", colours, alphas, ratios);
shadow.graphics.drawCircle(0,0, this.width/2);
shadow.graphics.endFill();
addChild(shadow);
}
public function orbit()
{
trace("internal y: "+y);
x = fokus.x + Math.sin(angleRad) * radius;
y = fokus.y + Math.cos(angleRad) * radius;
//var oldX:Number = x;
//var oldY:Number = y;
angleRad += angularSpeedRad;// * Math.PI /180;
//trace(angularSpeedRad);
}
public function updateShadow()
{
var dx:Number = sun.x - x;
var dy:Number = sun.y - y;
var rads = Math.atan2(dy, dx);
rads += Math.PI;
shadow.rotation = rads * 180 / Math.PI;
}
}