clean up code and add bullet class

master
Milan Suman 2022-08-04 12:38:59 +05:30
parent 150be16463
commit 9b37d4a245
2 changed files with 48 additions and 47 deletions

View File

@ -2,16 +2,12 @@ import * as paper from "paper";
let objects = [];
class Asteroid{
constructor(size, position, velocity=new paper.Point(0, 0)){
class Object{
constructor(size, position, velocity, path){
this.size = size;
this.position = position;
this.velocity = velocity;
this.path = new paper.Path.RegularPolygon(this.position, Math.max(5, Math.ceil(Math.random()*10)), this.size);
this.path.segments.forEach(elem => {
elem.point = elem.point.add(new paper.Point(Math.random()*15, Math.random()*15));
});
this.path.strokeColor = "#ffffff";
this.path = path;
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){
this.size = size;
this.position = position;
this.velocity = new paper.Point(0, 0);
this.nodes = [
new paper.Point(this.position.x, this.position.y-this.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)
let nodes = [
new paper.Point(position.x, position.y-size),
new paper.Point(position.x+size/2, position.y),
new paper.Point(position.x-size/2, position.y),
new paper.Point(position.x, position.y-size)
];
this.path = new paper.Path(this.nodes);
this.path.strokeColor = "#ffffff";
objects.push(this);
let path = new paper.Path(nodes);
path.strokeColor = "#ffffff";
path.fillColor = "#ffffffee";
super(size, position, new paper.Point(0, 0), path);
this.forward = nodes[0].subtract(position);
}
update(){
this.position = this.position.add(this.velocity);
this.path.position = this.position;
if(this.position.x > canvas.width + 30){
this.position = new paper.Point(-this.size, this.position.y);
}else if(this.position.x < -this.size){
this.position = new paper.Point(canvas.width+this.size, this.position.y);
rotate(angle){
this.forward.angle += angle;
this.path.rotate(angle)
}
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);
}
fire(){
new Bullet(this.position, this.forward);
}
};
class Bullet extends Object{
constructor(position, velocity){
let path = new paper.Path.Circle(position, 3);
path.fillColor = "#ffffff";
super(3, position, velocity, path);
}
}
//initialization
const canvas = document.getElementById("canvas");
canvas.width = window.innerWidth;
@ -89,12 +96,16 @@ const tool = new paper.Tool();
tool.on("keydown", (event) => {
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"){
ship.path.rotate(10);
ship.rotate(3);
}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