メニュー 閉じる

078. p5.jsで3Dシューティングゲーム用の戦闘機から弾を発射してみる

左右から弾を発射!

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

var fighter;

var bullets = [];

var fighterx;
var fightery;
var fighterz;

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, 2000.0);
  	pointLight (255, 0, 0, -2000.0, 0.0, -2000.0);
  	pointLight (0, 255, 0, 0.0, 2000.0, 2000.0);
  	pointLight (0, 255, 0, 0.0, -2000.0, -2000.0);
  	pointLight (255, 0, 0, 2000.0, 0.0, 2000.0);
  	pointLight (255, 0, 0, -2000.0, 0.0, -2000.0);
  	pointLight (0, 255, 0, 0.0, 2000.0, 2000.0);
  	pointLight (0, 255, 0, 0.0, -2000.0, -2000.0);

  	camera (0.0, 0.0, -250.0,
			0.0, 0.0, 0.0,
			0.0, 1.0, 0.0);

	translate (0.0, 0.0, 0.0);
	rotateX (0.4);
	rotateY (frameCount / 200.0);

	fighter.render ();

	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);
		}
	}

}

function FighterBase () {

	this.update = function () {

	}

	this.render = function () {

		translate (0.0, 0.0, 0.0);

		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, -20.0);
		rotateX (PI / 2.0);
		cylinder (2.0, 40.0);
		pop ();

		push ();
		translate (60.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 ();

	}

}

 

Posted in p5.js , processing