Cosmos
Enter key : creating a star
Space : making a blackhole(?) on mouse cursor
it is fail...X-(
/**
* Copyright gameegg ( http://wonderfl.net/user/gameegg )
* MIT License ( http://www.opensource.org/licenses/mit-license.php )
* Downloaded from: http://wonderfl.net/c/nkeq
*/
package {
import flash.ui.Keyboard;
import flash.display.Sprite;
import flash.events.Event;
import flash.events.MouseEvent;
import flash.events.KeyboardEvent;
public class Cosmos extends Sprite {
private var space:Sprite = new Sprite();
private var lineSpace:Sprite = new Sprite();
private var mouseSx:Number;
private var mouseSy:Number;
private var mouseDx:Number;
private var mouseDy:Number;
private var mouseDownBool:Boolean;
private var enterBool:Boolean;
private var spaceBool:Boolean;
private var a:Boolean;
private var circleColor:Number = 0x222222;
private var dx:Number=0;
private var dy:Number=0;
private var pvx:Number;
private var pvy:Number;
private var bh:Planet = new Planet(30,0,0,true);
public function Cosmos() {
bh.x = 465/2
bh.y = 465/2
a=false;
pvx=mouseX;
pvy=mouseY;
addChild(space);
addChild(lineSpace);
graphics.beginFill(0x111111);
graphics.drawRect(0,0,465,465);
lineSpace.graphics.lineStyle(1,0xaa0000);
space.graphics.beginFill(circleColor);
space.graphics.drawCircle(465/2,465/2,3);
stage.addEventListener(MouseEvent.MOUSE_DOWN,mouseDown);
stage.addEventListener(MouseEvent.MOUSE_MOVE,mouseMove);
stage.addEventListener(MouseEvent.MOUSE_UP,mouseUp);
stage.addEventListener(KeyboardEvent.KEY_UP,keyUp);
stage.addEventListener(KeyboardEvent.KEY_DOWN,keyDown);
addEventListener(Event.ENTER_FRAME,enterFrame);
}
private function enterFrame(e:Event):void{
//
for(var i:int = 0; i < space.numChildren; ++i){
for(var j:int = i+1; j < space.numChildren; ++j){
if(distance(Planet(space.getChildAt(i)),Planet(space.getChildAt(j))) > Planet(space.getChildAt(i)).r + Planet(space.getChildAt(j)).r){
gravity(Planet(space.getChildAt(i)),Planet(space.getChildAt(j)));
}
else{
fusionPlanet(Planet(space.getChildAt(i)),Planet(space.getChildAt(j)));
}
}
gravity(Planet(space.getChildAt(i)),bh);
if(!Planet(space.getChildAt(i)).blackhole){
Planet(space.getChildAt(i)).x += Planet(space.getChildAt(i)).pX/Planet(space.getChildAt(i)).m;
Planet(space.getChildAt(i)).y += Planet(space.getChildAt(i)).pY/Planet(space.getChildAt(i)).m;
}
else{
//Planet(space.getChildAt(i)).x = mouseX;
//Planet(space.getChildAt(i)).y = mouseY;
}
if(Planet(space.getChildAt(i)).x > 5000 || Planet(space.getChildAt(i)).y > 5000 || Planet(space.getChildAt(i)).y < -5000 || Planet(space.getChildAt(i)).x < -5000){
space.removeChild(Planet(space.getChildAt(i)));
}
/*
Planet(space.getChildAt(i)).pX += (232.5-Planet(space.getChildAt(i)).x)/100;
Planet(space.getChildAt(i)).pY += (232.5-Planet(space.getChildAt(i)).y)/100;
*/
if(!spaceBool && Planet(space.getChildAt(i)).blackhole) space.removeChildAt(i);
}
dx = mouseX-pvx;
dy = mouseY-pvy;
pvx=mouseX;
pvy=mouseY;
if(enterBool) createPlanet(mouseX,mouseY,1,0,0,false);
}
private function gravity(a:Planet, b:Planet):void{
var f:Number = (a.m*b.m)/((a.x-b.x)*(a.x-b.x) + (a.y-b.y)*(a.y-b.y));
var r:Number = Math.atan2(b.y-a.y,b.x-a.x);
a.pX += 10*f*Math.cos(r);
if(!b.blackhole)b.pX -= 10*f*Math.cos(r);
a.pY += 10*f*Math.sin(r);
if(!b.blackhole)b.pY -= 10*f*Math.sin(r);
}
private function createPlanet(ix:Number,iy:Number,im:Number,ipX:Number,ipY:Number,blackhole:Boolean):void{
var p:Planet = new Planet(im,ipX,ipY,blackhole);
p.x = ix;
p.y = iy;
space.addChild(p);
}
private function fusionPlanet(a:Planet,b:Planet):void{
if(!a.blackhole && !b.blackhole){
var distance:Number = distance(a,b);
a.x += (b.x-a.x)*(b.m/(a.m+b.m));
a.y += (b.y-a.y)*(b.m/(a.m+b.m));
a.pX += b.pX;
a.pY += b.pY;
space.removeChild(b);
a.massUp(b.m);
}
else if(a.blackhole){
a.massUp(b.m);
space.removeChild(b);
}
else{
b.massUp(a.m);
space.removeChild(a);
}
}
private function mouseDown(e:MouseEvent):void{
mouseSx = mouseX;
mouseSy = mouseY;
mouseDx = mouseX - mouseSx;
mouseDy = mouseY - mouseSy
lineSpace.graphics.moveTo(mouseSx,mouseSy);
mouseDownBool = true;
}
private function mouseMove(e:MouseEvent):void{
if(mouseDownBool){
lineSpace.graphics.clear();
lineSpace.graphics.lineStyle(2,0x880000);
lineSpace.graphics.moveTo(mouseSx,mouseSy);
lineSpace.graphics.lineTo(mouseX,mouseY);
mouseDx = mouseX - mouseSx;
mouseDy = mouseY - mouseSy
}
}
private function mouseUp(e:MouseEvent):void{
this.createPlanet(mouseSx,mouseSy,1,mouseDx/30,mouseDy/30,false);
lineSpace.graphics.clear();
mouseDownBool = false;
}
private function keyUp(e:KeyboardEvent):void{
if(e.keyCode == Keyboard.ENTER) enterBool = false;
else if(e.keyCode == Keyboard.SPACE) {
spaceBool = false;
if(a) {
space.graphics.beginFill(circleColor);
space.graphics.drawCircle(465/2,465/2,3)
}
a=false
}
}
private function keyDown(e:KeyboardEvent):void{
if(e.keyCode == Keyboard.ENTER) enterBool = true;
else if(e.keyCode == Keyboard.SPACE) {
spaceBool = true;
if(!a) {
space.graphics.clear();
}
//Planet(space.getChildAt(i)).x += Planet(space.getChildAt(i)).pX/Planet(space.getChildAt(i)).m;
//Planet(space.getChildAt(i)).y += Planet(space.getChildAt(i)).pY/Planet(space.getChildAt(i)).m;
a=true
}
}
private function distance(a:Planet,b:Planet):Number{
return Math.sqrt((a.x-b.x)*(a.x-b.x)+(a.y-b.y)*(a.y-b.y));
}
}
}
import flash.display.Sprite;
internal class Planet extends Sprite {
public var m:Number;
public var pX:Number;
public var pY:Number;
public var r:Number;
public var blackhole:Boolean;
public function Planet(im:Number, ipX:Number, ipY:Number, iblackhole:Boolean){
m=im;
pX=ipX;
pY=ipY;
blackhole = iblackhole;
if(!blackhole){
r=Math.pow(im,1/3);
graphics.beginFill(0xffffff);
graphics.drawCircle(0,0,r);
}
else {
r = 0;
}
}
public function massUp(im:Number):void{
m+=im;
if(!blackhole){
r=Math.pow(m,1/3);
graphics.beginFill(0xffffff);
graphics.drawCircle(0,0,r);
}
}
}