2016-09-26 80 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()而不是平方半徑來加速計算