clean up code and add bullet class
parent
150be16463
commit
9b37d4a245
83
src/index.js
83
src/index.js
|
@ -2,16 +2,12 @@ import * as paper from "paper";
|
||||||
|
|
||||||
let objects = [];
|
let objects = [];
|
||||||
|
|
||||||
class Asteroid{
|
class Object{
|
||||||
constructor(size, position, velocity=new paper.Point(0, 0)){
|
constructor(size, position, velocity, path){
|
||||||
this.size = size;
|
this.size = size;
|
||||||
this.position = position;
|
this.position = position;
|
||||||
this.velocity = velocity;
|
this.velocity = velocity;
|
||||||
this.path = new paper.Path.RegularPolygon(this.position, Math.max(5, Math.ceil(Math.random()*10)), this.size);
|
this.path = path;
|
||||||
this.path.segments.forEach(elem => {
|
|
||||||
elem.point = elem.point.add(new paper.Point(Math.random()*15, Math.random()*15));
|
|
||||||
});
|
|
||||||
this.path.strokeColor = "#ffffff";
|
|
||||||
objects.push(this);
|
objects.push(this);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -33,41 +29,52 @@ class Asteroid{
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
class Ship{
|
class Asteroid extends Object{
|
||||||
|
constructor(size, position, velocity=new paper.Point(0, 0)){
|
||||||
|
let path = new paper.Path.RegularPolygon(position, Math.max(5, Math.ceil(Math.random()*10)), size);
|
||||||
|
path.segments.forEach(elem => {
|
||||||
|
elem.point = elem.point.add(new paper.Point(Math.random()*15, Math.random()*15));
|
||||||
|
});
|
||||||
|
path.strokeColor = "#ffffff";
|
||||||
|
super(size, position, velocity, path);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
class Ship extends Object{
|
||||||
constructor(size, position){
|
constructor(size, position){
|
||||||
this.size = size;
|
let nodes = [
|
||||||
this.position = position;
|
new paper.Point(position.x, position.y-size),
|
||||||
this.velocity = new paper.Point(0, 0);
|
new paper.Point(position.x+size/2, position.y),
|
||||||
this.nodes = [
|
new paper.Point(position.x-size/2, position.y),
|
||||||
new paper.Point(this.position.x, this.position.y-this.size),
|
new paper.Point(position.x, position.y-size)
|
||||||
new paper.Point(this.position.x+this.size/2, this.position.y),
|
|
||||||
new paper.Point(this.position.x-this.size/2, this.position.y),
|
|
||||||
new paper.Point(this.position.x, this.position.y-this.size)
|
|
||||||
];
|
];
|
||||||
|
|
||||||
this.path = new paper.Path(this.nodes);
|
let path = new paper.Path(nodes);
|
||||||
this.path.strokeColor = "#ffffff";
|
path.strokeColor = "#ffffff";
|
||||||
objects.push(this);
|
path.fillColor = "#ffffffee";
|
||||||
|
|
||||||
|
super(size, position, new paper.Point(0, 0), path);
|
||||||
|
this.forward = nodes[0].subtract(position);
|
||||||
}
|
}
|
||||||
|
|
||||||
update(){
|
rotate(angle){
|
||||||
this.position = this.position.add(this.velocity);
|
this.forward.angle += angle;
|
||||||
this.path.position = this.position;
|
this.path.rotate(angle)
|
||||||
|
}
|
||||||
|
|
||||||
if(this.position.x > canvas.width + 30){
|
fire(){
|
||||||
this.position = new paper.Point(-this.size, this.position.y);
|
new Bullet(this.position, this.forward);
|
||||||
}else if(this.position.x < -this.size){
|
|
||||||
this.position = new paper.Point(canvas.width+this.size, this.position.y);
|
|
||||||
}
|
|
||||||
|
|
||||||
if(this.position.y > canvas.height + 30){
|
|
||||||
this.position = new paper.Point(this.position.x, -this.size);
|
|
||||||
}else if(this.position.y < -this.size){
|
|
||||||
this.position = new paper.Point(this.position.x, canvas.height+this.size);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
class Bullet extends Object{
|
||||||
|
constructor(position, velocity){
|
||||||
|
let path = new paper.Path.Circle(position, 3);
|
||||||
|
path.fillColor = "#ffffff";
|
||||||
|
super(3, position, velocity, path);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
//initialization
|
//initialization
|
||||||
const canvas = document.getElementById("canvas");
|
const canvas = document.getElementById("canvas");
|
||||||
canvas.width = window.innerWidth;
|
canvas.width = window.innerWidth;
|
||||||
|
@ -89,12 +96,16 @@ const tool = new paper.Tool();
|
||||||
|
|
||||||
tool.on("keydown", (event) => {
|
tool.on("keydown", (event) => {
|
||||||
if(event.character == "w"){
|
if(event.character == "w"){
|
||||||
ship.velocity = ship.velocity.add(ship.nodes[0].subtract(new paper.Point(canvas.width/2, 0)).normalize());
|
ship.velocity = ship.velocity.add(ship.forward.normalize());
|
||||||
}
|
}
|
||||||
|
|
||||||
if(event.key == "right"){
|
if(event.key == "right"){
|
||||||
ship.path.rotate(10);
|
ship.rotate(3);
|
||||||
}else if(event.key == "left"){
|
}else if(event.key == "left"){
|
||||||
ship.path.rotate(-10);
|
ship.rotate(-3);
|
||||||
|
}
|
||||||
|
|
||||||
|
if(event.key == "space"){
|
||||||
|
ship.fire();
|
||||||
}
|
}
|
||||||
});
|
});
|
File diff suppressed because one or more lines are too long
Loading…
Reference in New Issue