Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- function d(x1, y1, x2, y2){
- return Math.sqrt((x2-x1)*(x2-x1) + (y2-y1)*(y2-y1));
- }
- class Segment{
- constructor(bx, by, ex, ey){
- this.bx = bx;
- this.by = by;
- this.ex = ex;
- this.ey = ey;
- this.dist = d(bx, by, ex, ey);
- }
- }
- class Route{
- constructor(n, color, bx=0, by=0, ex=800, ey=600){
- this.s = [];
- this.color = color;
- for (let i=0; i<n; ++i){
- let bx_, by_, ex_, ey_;
- if (i==0){
- bx_ = bx;
- by_ = by;
- }
- else {
- bx_ = this.s[i-1].ex;
- by_ = this.s[i-1].ey;
- }
- ex_ = Math.random()*800;
- ey_ = Math.random()*600;
- if (i==n-1){
- ex_ = ex;
- ey_ = ey;
- }
- this.s[i] = new Segment(bx_, by_, ex_, ey_);
- }
- }
- draw(){
- context.strokeStyle = this.color;
- context.beginPath();
- context.moveTo(this.s[0].bx, this.s[0].by);
- for (let i=0; i<this.s.length; ++i){
- context.lineTo(this.s[i].ex, this.s[i].ey);
- }
- context.stroke();
- }
- }
- class Enemy{
- constructor(){
- this.r_ind = Math.floor(Math.random()*r.length);
- this.s_ind = 0;
- this.d = 0;
- this.calcxy();
- this.image = duckTarget;
- this.sx = 50;
- this.sy = 50;
- this.speed = 5;
- this.finish = false;
- this.hp = 100;
- }
- calcxy(){
- if (this.finish || this.hp <= 0) return;
- this.x = r[this.r_ind].s[this.s_ind].bx;
- this.y = r[this.r_ind].s[this.s_ind].by;
- this.x += (r[this.r_ind].s[this.s_ind].ex - r[this.r_ind].s[this.s_ind].bx)*this.d/r[this.r_ind].s[this.s_ind].dist;
- this.y += (r[this.r_ind].s[this.s_ind].ey - r[this.r_ind].s[this.s_ind].by)*this.d/r[this.r_ind].s[this.s_ind].dist;
- }
- update(){
- if (this.finish || this.hp <= 0) return;
- this.d += this.speed;
- if (this.d > r[this.r_ind].s[this.s_ind].dist){
- this.d -= r[this.r_ind].s[this.s_ind].dist;
- this.s_ind++;
- if (this.s_ind >= r[this.r_ind].s.length){
- this.finish = true;
- }
- }
- this.calcxy();
- }
- draw(){
- if (this.finish || this.hp <= 0) return;
- drawImage(this.image, this.x-this.sx/2, this.y-this.sy/2, this.sx, this.sy);
- }
- }
- class Tower{
- constructor(x, y){
- this.x = x;
- this.y = y;
- this.r = 200;
- this.target = [];
- this.dmg = 0.5;
- }
- shoot(){
- this.target = [];
- for (let i=0; i<e.length; ++i){
- if (d(this.x, this.y, e[i].x, e[i].y) < this.r && !e[i].finish && e[i].hp > 0){
- this.target.push(i);
- e[i].hp -= this.dmg;
- }
- }
- }
- update(){
- this.shoot();
- }
- draw(){
- drawImage(crystal, this.x-25, this.y-25, 50, 50);
- context.strokeStyle = "red";
- context.beginPath();
- for (let i=0; i<this.target.length; ++i){
- context.moveTo(this.x, this.y);
- context.lineTo(e[this.target[i]].x, e[this.target[i]].y);
- }
- context.stroke();
- }
- }
- var r = [new Route(3, "blue"), new Route(5, "red"), new Route(7, "green")];
- var e = [];
- var tower = [];
- var t = 0;
- function update() {
- ++t;
- if (t%1==0){
- e.push(new Enemy())
- }
- for (let i=0; i<e.length; ++i){
- e[i].update();
- }
- for (let i=0; i<tower.length; ++i){
- tower[i].update();
- }
- }
- function draw() {
- for (let i=0; i<r.length; ++i){
- r[i].draw();
- }
- for (let i=0; i<e.length; ++i){
- e[i].draw();
- }
- for (let i=0; i<tower.length; ++i){
- tower[i].draw();
- }
- };
- function keyup(key) {
- // Show the pressed keycode in the console
- console.log("Pressed", key);
- };
- function mouseup() {
- tower.push(new Tower(mouseX, mouseY));
- };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement