メニュー 閉じる

079. p5.jsで3Dシューティングゲーム用の戦闘機を空間に飛ばしてみる

宇宙空間を進みます・・・途中で異常に重くなる・・・。

http://www.velvet-number.com/p5_test/test056/

var fighter;

var bullets = [];

var stars = [];

var fighterx;
var fightery;
var fighterz;

var main_z = 0.0;

var dragged = false;

function setup () {

	pixelDensity (displayDensity ());
	createCanvas (windowWidth, windowHeight, WEBGL);
	colorMode (RGB, 256);
	background (0);

	fighter = new FighterBase ();

}

function draw () {

	background (0);

	ambientLight (10, 120, 40);
  	pointLight (255, 0, 0, 2000.0, 0.0, main_z + 2000.0);
  	pointLight (255, 0, 0, -2000.0, 0.0, main_z - 2000.0);
  	pointLight (0, 255, 0, 0.0, 2000.0, main_z + 2000.0);
  	pointLight (0, 255, 0, 0.0, -2000.0, main_z - 2000.0);
  	pointLight (255, 0, 0, 2000.0, 0.0, main_z + 2000.0);
  	pointLight (255, 0, 0, -2000.0, 0.0, main_z - 2000.0);
  	pointLight (0, 255, 0, 0.0, 2000.0, main_z + 2000.0);
  	pointLight (0, 255, 0, 0.0, -2000.0, main_z - 2000.0);

  	camera (-1.0 * (-mouseX + width / 2.0), -mouseY + height / 2.0, main_z - 250.0,
			0.0, 0.0, 0.0,
			0.0, 1.0, 0.0);

	translate (0.0, 0.0, main_z);
	rotateY (PI);

	for (var i = 0; i < 10; i++) {
		stars.push (new StarBase (random (-width / 2.0, width / 2.0), random (-height / 2.0, height / 2.0), main_z));
	}

	for (var i = 0; i < stars.length; i++) {
		stars[i].update ();
		stars[i].render ();
		if (stars[i].life_flag == false) {
			stars.splice (i, 1);
		}
	}

	fighter.render (main_z);

	if (dragged == true) {
		bullets.push (new BulletBase (60.0, 0.0, 0.0, random (-10.0, -60.0), 0.0, false));
		bullets.push (new BulletBase (-60.0, 0.0, 0.0, random (-10.0, -60.0), 0.0, false));
	}

	for (var i = bullets.length - 1; i >= 0; i--) {
		bullets[i].update ();
		bullets[i].render ();
		if (bullets[i].life_flag == false) {
			bullets.splice (i, 1);
		}
	}

	main_z -= 10.0;

}

function FighterBase () {

	this.update = function () {

	}

	this.render = function (myz) {

		stroke (0);
		strokeWeight (0.2);

		push ();
		translate (0.0, 0.0, 0.0);
		box (80.0, 10.0, 100.0);
		pop ();

		push ();
		translate (0.0, 0.0, -60.0);
		box (40.0, 5.0, 100.0);
		pop ();

		push ();
		translate (-60.0, 0.0, 0.0);
		box (40.0, 5.0, 40.0);
		pop ();

		push ();
		translate (60.0, 0.0, 0.0);
		box (40.0, 5.0, 40.0);
		pop ();

		push ();
		translate (-60.0, 0.0, -20.0);
		rotateX (PI / 2.0);
		cylinder (2.0, 40.0);
		pop ();

		push ();
		translate (60.0, 0.0, -20.0);
		rotateX (PI / 2.0);
		cylinder (2.0, 40.0);
		pop ();

		push ();
		translate (-30.0, -5.0, 50.0);
		box (4.0, 20.0, 40.0);
		pop ();

		push ();
		translate (30.0, -5.0, 50.0);
		box (4.0, 20.0, 40.0);
		pop ();

	}

}

function mousePressed () {

	dragged = true;

}

function mouseReleased () {

	dragged = false;

}

function keyPressed () {

	dragged = true;

}

function keyReleased () {

	dragged = false;
	
}

function BulletBase (b_x, b_y, b_z, b_force, b_rad, b_flag) {

	this.location = new p5.Vector (b_x, b_y, b_z);
	this.velocity = new p5.Vector (0.0, 0.0, b_force);
	this.life_time = b_force;
	this.life_flag = true;

	this.update = function () {

		this.life_time *= 0.98;
		this.velocity.mult (0.98);
		this.location.add (this.velocity);

		if (abs (this.life_time) < 10.0) {
			this.life_flag = false;
		}

	}

	this.render = function () {

		push ();
		translate (this.location.x, this.location.y, this.location.z);
		noStroke ();
		box (2);
		pop ();

	}

}

function StarBase (s_x, s_y, s_z) {

	this.life_time = 0.0;
	this.life_flag = true;

	this.posx = s_x;
	this.posy = s_y;
	this.posz = s_z;

	this.update = function () {

		this.posz += 100.0;

		if (this.life_time > 200.0) {
			this.life_flag = false;
		}

		this.life_time += 1.2;

	}

	this.render = function () {

		push ();
		translate (this.posx, this.posy, this.posz);
		noStroke ();
		sphere (4);
		pop ();

	}

}

 

Posted in p5.js , processing