forked from: lifegame
昔作ったライフゲームっぽいもの
// forked from undo's lifegame
// 昔作ったライフゲームっぽいもの
package{
import flash.display.*;
import flash.text.*;
import flash.events.Event;
import flash.geom.Point;
public class main extends Sprite{
private var particles:Array;
private var numParticles:uint = 50;
private var minDist:Number;
private var springAmount:Number = .0015;//これを.001〜.002くらいで調節
private var damage:Number = 1;
private var ether:Number = 2;
private var MaxMP:Number = 100;
private var defHP:Number = 100;
private var maxChild:uint = 3;
private var maxConn:uint = 10;
public function main(){
minDist = stage.stageWidth/10;
//男女ノードを作ってparticles配列に入れる
particles = new Array();
for (var i:uint = 0; i < numParticles; i++) {
makeNode(Math.random() * stage.stageWidth, Math.random() * stage.stageHeight);
}
addEventListener(Event.ENTER_FRAME, onEnterFrame);
}
private function onEnterFrame(event:Event):void {
graphics.clear();
//各ノードの処理。死亡判定するのでケツから処理
for (var i:Number = numParticles - 1; i >= 0; i--) {
var particle:Ball = particles[i];
//体力が無くなっていたら死亡
//子供が3回以上生まれていても死亡
if (particle.HP <= 0 || particle.bornChild >= 3) {
particles.splice(i,1);
removeChild(particle);
numParticles--;
}
else {
//体力が減る
particle.HP -= damage;
//体力によって色が薄くなる
particle.alpha = particle.HP/defHP;
particle.scaleX = particle.scaleY = 4 - particle.alpha*3;
//位置を移動
particle.x += particle.vx;
particle.y += particle.vy;
//ステージをはみ出したら反対側から戻す
if (particle.x - particle.width> stage.stageWidth) {
particle.x = -particle.width;
}
else if (particle.x < -particle.width) {
particle.x = stage.stageWidth +particle.width;
}
if (particle.y -particle.height > stage.stageHeight) {
particle.y = -particle.height;
}
else if (particle.y < -particle.height) {
particle.y = stage.stageHeight +particle.height;
}
}
}//各ノードの処理終わり。
//他のノードとの処理をする関数を呼び出す
for (i=0; i < numParticles - 1; i++) {
var partA:Ball = particles[i];
partA.conn = 0;
for (var j:uint = i + 1; j < numParticles; j++) {
var partB:Ball = particles[j];
spring(partA, partB);
}
}
}
//2つのノード間の距離を計算して色々処理するspring()関数
private function spring(partA:Ball, partB:Ball):void {
//距離を計算
var dx:Number = partB.x - partA.x;
var dy:Number = partB.y - partA.y;
var dist:Number = Math.sqrt(dx * dx + dy * dy);
//一定距離内(minDist定数)だった場合
if (dist < minDist) {
partA.conn ++;
if(partA.conn == maxConn){
partA.HP = 0;
}
graphics.moveTo(partA.x, partA.y);
//加速度を計算(距離*ばね定数)
var ax:Number = dx * springAmount;
var ay:Number = dy * springAmount;
if (partA.sex != partB.sex) {
//性別が違う場合
//お互いのMPが一定値以上の場合は子が誕生する。
if(partA.MP > MaxMP && partB.MP > MaxMP){
makeNode((partA.x + partB.x)/2,(partA.y + partB.y)/2);
numParticles ++;
partA.MP = partB.MP = 0;
partA.bornChild++;
partB.bornChild++;
}
//引き合い、MPとHPを加算する。
//graphics.lineStyle(1, 0xffffff, 1 - dist / minDist);
//graphics.lineTo(partB.x, partB.y);
partA.vx += ax;
partA.vy += ay;
partB.vx -= ax;
partB.vy -= ay;
partA.HP += damage/4;
partB.HP += damage/4;
partA.MP += ether;
partB.MP += ether;
}
else {
//性別が同じ場合は反発し、HPを加算。
//graphics.lineStyle(1, 0xff00ff, 1 - dist / minDist);
//graphics.lineTo(partB.x, partB.y);
partA.vx -= ax;
partA.vy -= ay;
partB.vx += ax;
partB.vy += ay;
partA.HP -= damage/4;
partB.HP -= damage/4;
}
}
}
//ノードを作るmakeNode()関数
private function makeNode(posX:Number, posY:Number){
var particle:Ball =new Ball(3,Math.floor(Math.random()*2), defHP);
particle.x = posX;
particle.y = posY;
particle.vx = Math.random() * minDist/50-minDist/100;
particle.vy = Math.random() * minDist/50-minDist/100;
addChild(particle);
particles.push(particle);
}
}
}
import flash.display.Sprite;
class Ball extends Sprite {
public var radius:Number;
public var color:uint;
public var vx:Number = 0;
public var vy:Number = 0;
public var mass:Number = 1;
public var age:uint = 0;
public var sex:Boolean;/*男はfalse女はtrue*/
public var HP:Number = 100;
public var MP:Number = 0;
public var bornChild:Number = 0;
public var conn:Number = 0;
public var id:Number;
public function Ball(radius:Number = 3,sex:Boolean = false,HP:Number = 100) {
this.radius = radius;
this.sex = sex;
this.HP = HP;
if (sex == true) {
color = 0x007fff;
graphics.beginFill(color);
graphics.drawRect(-radius, -radius/4, radius*2, radius/2);
graphics.beginFill(color);
graphics.drawRect(-radius/4, -radius, radius/2, radius*2);
graphics.endFill();
}
else {
color = 0xff007f;
graphics.lineStyle(radius/2,color);
graphics.drawCircle(0,0,radius);
graphics.endFill();
}
}
}