Ignore:
Timestamp:
01/26/11 02:40:22 (13 years ago)
Author:
atzm
Message:
  • fixed small bug
  • add bullet frame
  • parameter adjust
File:
1 edited

Legend:

Unmodified
Added
Removed
  • pycodeshooter/trunk/shooter/bullet.js

    r105 r106  
    1414 */ 
    1515 
    16 var Bullet = function(size, color, x, y, dir, speed) { 
     16var Bullet = function(size, color, frame, x, y, dir, speed) { 
    1717    this.size  = size; 
    1818    this.color = color; 
     19    this.frame = frame; 
    1920    this.x     = x; 
    2021    this.y     = y; 
     
    2223    this.speed = speed; 
    2324 
     25    this.getSize = function() { 
     26        return this.size; 
     27    }; 
     28    this.getColor = function() { 
     29        return this.color; 
     30    }; 
     31    this.next = function() { 
     32        var pos = this.calcNext(); 
     33        this.x = pos[0]; 
     34        this.y = pos[1]; 
     35    }; 
     36    this.vanished = function(width, height) { 
     37        return (width < this.x || height < this.y || this.x < 0 || this.y < 0); 
     38    }; 
    2439    this.draw = function(ctx) { 
     40        if (this.frame) { 
     41            var pos = this.calcNext(); 
     42            var dir = Math.atan2(pos[0] - this.x, this.y - pos[1]); 
     43 
     44            ctx.save(); 
     45 
     46            if (this.frame.style == "rect") { 
     47                ctx.beginPath(); 
     48                ctx.strokeStyle = this.frame.color; 
     49                ctx.translate(this.x, this.y); 
     50                ctx.rotate(dir); 
     51                ctx.rect(-this.frame.width / 2, -this.frame.height / 2, 
     52                         this.frame.width, this.frame.height); 
     53                ctx.stroke(); 
     54                ctx.closePath(); 
     55            } 
     56 
     57            ctx.restore(); 
     58        } 
     59 
    2560        ctx.beginPath(); 
    2661        ctx.fillStyle = this.getColor(); 
     
    2964        ctx.closePath(); 
    3065    }; 
    31     this.vanished = function(width, height) { 
    32         return (width < this.x || height < this.y || this.x < 0 || this.y < 0); 
    33     }; 
    34     this.getSize = function() { 
    35         return this.size; 
    36     }; 
    37     this.getColor = function() { 
    38         return this.color; 
    39     }; 
    4066}; 
    4167 
    42 var LinerBullet = function(size, color, x, y, dir, speed) { 
    43     var that = new Bullet(size, color, x, y, dir, speed); 
     68var LinerBullet = function(size, color, frame, x, y, dir, speed) { 
     69    var that = new Bullet(size, color, frame, x, y, dir, speed); 
    4470 
    4571    that.deltaBaseX = Math.cos(that.dir); 
    4672    that.deltaBaseY = Math.sin(that.dir); 
    4773 
    48     that.next = function() { 
    49         this.x += this.getDeltaX(); 
    50         this.y += this.getDeltaY(); 
     74    that.calcNext = function() { 
     75        return [this.x + this.getDeltaX(), this.y + this.getDeltaY()]; 
    5176    }; 
    5277    that.getDeltaX = function() { 
     
    6085}; 
    6186 
    62 var AxisExtendBullet = function(size, color, x, y, dir, speed) { 
    63     var that = new LinerBullet(size, color, x, y, dir, speed); 
     87var AxisExtendBullet = function(size, color, frame, x, y, dir, speed) { 
     88    var that = new LinerBullet(size, color, frame, x, y, dir, speed); 
    6489 
    6590    that.dx = that.speed * that.deltaBaseX / 1.5; 
     
    80105}; 
    81106 
    82 var XYExtendBullet = function(size, color, x, y, dir, speed) { 
    83     var that = new AxisExtendBullet(size, color, x, y, dir, speed); 
     107var XYExtendBullet = function(size, color, frame, x, y, dir, speed) { 
     108    var that = new AxisExtendBullet(size, color, frame, x, y, dir, speed); 
    84109 
    85110    that.i = 1; 
     
    94119}; 
    95120 
    96 var XExtendBullet = function(size, color, x, y, dir, speed) { 
    97     var that = new AxisExtendBullet(size, color, x, y, dir, speed); 
     121var XExtendBullet = function(size, color, frame, x, y, dir, speed) { 
     122    var that = new AxisExtendBullet(size, color, frame, x, y, dir, speed); 
    98123 
    99124    that.i = 1; 
     
    108133}; 
    109134 
    110 var YExtendBullet = function(size, color, x, y, dir, speed) { 
    111     var that = new AxisExtendBullet(size, color, x, y, dir, speed); 
     135var YExtendBullet = function(size, color, frame, x, y, dir, speed) { 
     136    var that = new AxisExtendBullet(size, color, frame, x, y, dir, speed); 
    112137 
    113138    that.i = 1; 
     
    122147}; 
    123148 
    124 var CurveBullet = function(size, color, x, y, dir, speed) { 
    125     var that = new Bullet(size, color, x, y, dir, speed); 
     149var CurveBullet = function(size, color, frame, x, y, dir, speed) { 
     150    var that = new Bullet(size, color, frame, x, y, dir, speed); 
    126151 
    127152    that.delta = 1 / (that.speed * 100); 
    128153    that.i     = 1; 
    129154 
    130     that.next = function() { 
    131         this.x   += Math.cos(this.dir) * this.i; 
    132         this.y   += Math.sin(this.dir) * this.i; 
     155    that.calcNext = function() { 
     156        var x = this.x + Math.cos(this.dir) * this.i; 
     157        var y = this.y + Math.sin(this.dir) * this.i; 
    133158        this.dir += (this.delta / this.i) * this.getSign(); 
    134159        this.i   += (this.delta / this.i); 
     160        return [x, y]; 
    135161    }; 
    136162 
     
    138164}; 
    139165 
    140 var LeftCurveBullet = function(size, color, x, y, dir, speed) { 
    141     var that = new CurveBullet(size, color, x, y, dir, speed); 
     166var LeftCurveBullet = function(size, color, frame, x, y, dir, speed) { 
     167    var that = new CurveBullet(size, color, frame, x, y, dir, speed); 
    142168 
    143169    that.getSign = function() { 
     
    148174}; 
    149175 
    150 var RightCurveBullet = function(size, color, x, y, dir, speed) { 
    151     var that = new CurveBullet(size, color, x, y, dir, speed); 
     176var RightCurveBullet = function(size, color, frame, x, y, dir, speed) { 
     177    var that = new CurveBullet(size, color, frame, x, y, dir, speed); 
    152178 
    153179    that.getSign = function() { 
Note: See TracChangeset for help on using the changeset viewer.