2016-09-26 54 views
1

我会保持这个简短的,我做一个塔防游戏作为一个迷你项目,而我有一些空闲时间,我想弄清楚如何我可以实施塔可以在使用dist进入射程时拍摄敌人,但我不知道如何执行使用enimies位置和塔位置的方法。我有CreepSprites的ArrayList和塔加工塔防御游戏 - 塔攻击敌人

CreepSprite[] CreepSprites; 
ArrayList<Tower> AllTowers = new ArrayList<Tower>(); 
ArrayList<Creep> AllCreeps = new ArrayList<Creep>(); 

如果有人可以给我一些指导做好,我怎么会去有关使能够拍摄毛骨悚然这将是巨大的塔楼,即使它没有摆脱只要能够射击他们就会很棒。

干杯

+1

我不完全明白你的要求。你很可能有一个塔和一个蠕变的x和y位置。所以你首先需要计算距离。这很简单:'''xd = x2-x1,yd = y2-y1,dist = sqrt(xd * xd + yd * yd)'''接下来你需要一个更新循环,它涉及范围或从中走出。如果是这样,塔可能会或可能不会开枪。 –

+0

我正在考虑制作一个方法,并将其放入以60fps运行的绘图类中,并且该方法中有if语句 – Michael

回答

1

您可以使用Point2D类来表示x,y坐标将图的。该课程获得了预先实施的distance method,可以在半径范围内进行检查。

+1

请注意,这是[tag:processing]问题。处理已经有一个'dist()'函数和一个'PVector'类来表示点。 –

+0

W。。没有看到。感谢提示。 – GoneUp

6

@ GoneUp的答案是在正确的方向。在处理您有一个名为PVector类,它提供了一个距离的方法,以及:dist()

PVector towerPos = new PVector(100, 100); 
PVector enemyPos = new PVector(300, 300); 

float towerAttackRadius = 200; 


void setup() { 
    size(400, 400); 
    rectMode(CENTER); 
    strokeWeight(3); 
} 
void draw() { 
    background(255); 
    //draw tower 
    noFill(); 

    //check range 
    if(towerPos.dist(enemyPos) < towerAttackRadius){ 
    //tower engaged, draw in green 
    stroke(0,192,0); 
    }else{ 
    //tower in idle mode, draw in blue 
    stroke(0, 0, 192); 
    } 

    //visualise tower attack radius 
    //(towerAttackRadius * 2 -> radius * 2 = diameter (width/height)) 
    ellipse(towerPos.x, towerPos.y, towerAttackRadius * 2, towerAttackRadius * 2); 
    //visualise tower 
    rect(towerPos.x, towerPos.y, 30, 30); 
    //draw enemy 
    stroke(192, 0, 0); 
    rect(enemyPos.x, enemyPos.y, 10, 10); 
    //instructions 
    fill(0); 
    text("click and drag to move enemy",10,15); 
} 

void mouseDragged() { 
    enemyPos.set(mouseX, mouseY); 
} 

我热烈推荐丹尼尔Shiffman的Nature of Code chapter on Vectors。 这是一种线性代数,但它非常有用,特别是对于那些值得早期掌握的游戏。

例如,要拍摄子弹,您需要锻炼朝向。 你可以通过使用矢量减法来做到这一点。

另外,您可能想要控制子弹在屏幕上向该方向移动的速度。这也可以通过归一化向量做:保持它的方向,但减少它的大小为1.0:

vector normalization diagram from https://www.mathworks.com/matlabcentral/mlc-downloads/downloads/submissions/36248/versions/1/screenshot.png

那之后很容易扩展(乘)矢量到任何你想要的大小。

如果你知道塔的位置,你只需要添加这个缩放速度来计算每一帧应该绘制的子弹的位置。

PVector已经有一个功能:setMag()。 (设置mag是设置的幅度(或向量长度)的缩写)

它还提供了一个便于锻炼角度的功能。

这里是概念的基本证明:

PVector towerPos = new PVector(100, 100); 
PVector enemyPos = new PVector(300, 300); 

float towerAttackRadius = 300; 

ArrayList<Bullet> bullets = new ArrayList<Bullet>(); 

void setup() { 
    size(400, 400); 
    rectMode(CENTER); 
    strokeWeight(3); 
} 
void draw() { 
    background(255); 

    //check if an enemy is within range using dist() 
    //if the distance is smaller than the radius, attack! 
    if(towerPos.dist(enemyPos) < towerAttackRadius){ 

    //hacky frame based counter: please use a millis() based timer instead 
    //shoot every 30 frames 
    if(frameCount % 30 == 0){ 
     shoot(); 
    } 

    } 

    //update bullets 
    for(Bullet b : bullets) { 
    b.update(); 
    } 

    //draw tower 
    noFill(); 
    stroke(0, 0, 192); 
    //visualise tower attack radius 
    //(towerAttackRadius * 2 -> radius * 2 = diameter (width/height)) 
    ellipse(towerPos.x, towerPos.y, towerAttackRadius * 2, towerAttackRadius * 2); 
    //visualise tower 
    rect(towerPos.x, towerPos.y, 30, 30); 
    //draw enemy 
    stroke(192, 0, 0); 
    rect(enemyPos.x, enemyPos.y, 10, 10); 
    //instructions 
    fill(0); 
    text("click and drag to move enemy",10,15); 
} 

void mouseDragged() { 
    enemyPos.set(mouseX, mouseY); 
} 

void shoot(){ 
    //make a new Bullet pointing from the tower to the enemy 
    Bullet b = new Bullet(towerPos.x,towerPos.y,enemyPos.x,enemyPos.y); 
    //add it to the list of bullets (for updates) 
    bullets.add(b); 

    println(b); 
} 

class Bullet { 
    //where does the bullet shoot from (and it's current position) 
    PVector position = new PVector(); 
    //where does the bullet shooting towards 
    PVector target = new PVector(); 
    //how fast does the bullet move on screen 
    float speed = 1.2; 
    //how large goes the bullet appear on screen 
    float size = 10; 

    //bullet velocity 
    PVector velocity; 

    Bullet(float startX,float startY, float endX, float endY) { 
    position.set(startX,startY); 
    target.set(endX,endY); 

    //compute the difference vector (start to end) = direction 
    velocity = PVector.sub(target,position); 

    //normalize the vector = same direction but magnitude of 1 -> makes it easy to scale 
    //velocity.normalize(); 

    //scale by the speed to move on screen) 
    //normalize + multiple = resize the vector -> same direction, different length 
    //velocity.mult(speed); 

    //or do both normalize and multiple using setMag() 
    velocity.setMag(speed); 
    } 
    void update() { 
    //update position based on velocity (simply add velocity to current position) 
    position.add(velocity); 

    //render 
    //compute rotation angle from velocity (implementation is 2D only btw) 
    float angle = velocity.heading(); 
    pushMatrix(); 
    translate(position.x,position.y); 
    rotate(angle); 
    stroke(0); 
    line(0,0,size,0); 
    popMatrix(); 
    } 

    String toString(){ 
    return position+"->"+target; 
    } 
} 

preview

实际上,你可以用预览波纹管玩:

var towerPos,enemyPos; 
 

 
var towerAttackRadius = 300; 
 

 
var bullets = []; 
 

 
function setup() { 
 
    createCanvas(400, 400); 
 
    rectMode(CENTER); 
 
    strokeWeight(3); 
 
    
 
    
 
    towerPos = createVector(100, 100); 
 
    enemyPos = createVector(300, 300); 
 
} 
 
function draw() { 
 
    background(255); 
 
    
 
    //check if an enemy is within range using dist() 
 
    //if the distance is smaller than the radius, attack! 
 
    if(towerPos.dist(enemyPos) < towerAttackRadius){ 
 
    
 
    //hacky frame based counter: please use a millis() based timer instead 
 
    //shoot every 30 frames 
 
    if(frameCount % 30 === 0){ 
 
     shoot(); 
 
    } 
 
    
 
    } 
 
    
 
    //update bullets 
 
    for(var i = 0; i < bullets.length; i++) { 
 
    bullets[i].update(); 
 
    } 
 

 
    //draw tower 
 
    noFill(); 
 
    stroke(0, 0, 192); 
 
    //visualise tower attack radius 
 
    //(towerAttackRadius * 2 -> radius * 2 = diameter (width/height)) 
 
    ellipse(towerPos.x, towerPos.y, towerAttackRadius * 2, towerAttackRadius * 2); 
 
    //visualise tower 
 
    rect(towerPos.x, towerPos.y, 30, 30); 
 
    //draw enemy 
 
    stroke(192, 0, 0); 
 
    rect(enemyPos.x, enemyPos.y, 10, 10); 
 
    //instructions 
 
    noStroke(); 
 
    fill(0); 
 
    text("click and drag to move enemy",10,15); 
 
} 
 

 
function mouseDragged() { 
 
    enemyPos.set(mouseX, mouseY); 
 
} 
 

 
function shoot(){ 
 
    //make a new Bullet pointing from the tower to the enemy 
 
    var b = new Bullet(towerPos.x,towerPos.y,enemyPos.x,enemyPos.y); 
 
    //add it to the list of bullets (for updates) 
 
    bullets.push(b); 
 
} 
 

 
function Bullet(startX,startY,endX,endY) { 
 
    //where does the bullet shoot from (and it's current position) 
 
    this.position = createVector(startX,startY); 
 
    //where does the bullet shooting towards 
 
    this.target = createVector(endX,endY); 
 
    //how fast does the bullet move on screen 
 
    this.speed = 1.2; 
 
    //how large goes the bullet appear on screen 
 
    this.size = 10; 
 

 
    //compute the difference vector (start to end) = direction 
 
    this.velocity = p5.Vector.sub(this.target,this.position); 
 
    //normalize the vector = same direction but magnitude of 1 -> makes it easy to scale 
 
    this.velocity.normalize(); 
 
    //scale by the speed to move on screen) 
 
    //normalize + multiple = resize the vector -> same direction, different length 
 
    this.velocity.mult(this.speed); 
 
    
 
    this.update = function() { 
 
    //update position based on velocity (simply add velocity to current position) 
 
    this.position.add(this.velocity); 
 
    
 
    //render 
 
    //compute rotation angle from velocity (implementation is 2D only btw) 
 
    var angle = this.velocity.heading(); 
 
    push(); 
 
    translate(this.position.x,this.position.y); 
 
    rotate(angle); 
 
    stroke(0); 
 
    line(0,0,this.size,0); 
 
    pop(); 
 
    } 
 
    
 
} 
 

 

 
//http://stackoverflow.com/questions/39698472/processing-tower-defence-game-towers-attacking-enemies
<script src="https://cdnjs.cloudflare.com/ajax/libs/p5.js/0.5.3/p5.min.js"></script>

有PVectors乐趣!:) 一个重要的注意: 上述代码是一个概念的证明,并没有优化。 从长远来看,塔和敌人会减速。 一旦你获得了数学/代码的权利,你就可以开始做了一些改进:

  • 管理子弹/实例(例如重复使用的子弹是关闭屏幕 ,而不是创建新实例的所有时间)
  • 改为使用平方距离magSq()而不是平方半径来加速计算