2017-06-06 42 views
0

我目前正在對一所學校的項目,是由於週五&我做過了很多事情要做。 這個任務是使用Monogame製作XNA框架的視頻遊戲。我目前正在處理碰撞。如何在繼承類中使用抽象方法?

爲gameobjects的結構看起來有點像這樣: enter image description here

對於碰撞,我有一個簡單的碰撞類

 class Collision{ 

public GameObject Other; 

public GameObject Obj1; 

public Collision(GameObject obj1, GameObject other) 
{ 
    Other = other; 
    Obj1 = obj1; 
}} 

的碰撞是一個靜態方法在遊戲對象類處理:

public static void UpdateCollisions() 
{ 
    //Empty the list 
    AllCollisions.Clear(); 

    for (int a = 0; a < AllGameObjectsWithCollision.Count; a++) 
    { 
     GameObject obja = AllGameObjectsWithCollision[a]; 
     for (int b = 0; b < AllGameObjectsWithCollision.Count; b++) 
     { 
      GameObject objb = AllGameObjectsWithCollision[b]; 

      if (obja.Mask != null & objb.Mask!= null && obja != objb) 
      { 
       if (obja.Mask.CollisionRectangle.Intersects(objb.Mask.CollisionRectangle)) 
        AllCollisions.Add(new Collision(obja, objb)); 
      } 
     } 
    } 

} 

這遠遠它的工作,遊戲是找到的所有碰撞喜歡它應該。但是現在我需要讓我的對象知道他們正在碰撞,並告訴他們該做什麼。 對於這一點,我做了實體類的抽象能夠聲明稱「OnCollision(碰碰撞)」

abstract class Entity : GameObject 
 
{ 
 
    public float Health; 
 
    public float MaxHealth; 
 
    public bool Alive; 
 
    public float OriginalDmg; 
 
    public float Dmg; 
 

 
    public abstract void OnCollision(Collision collision); 
 
}

然後我重寫方法中的類抽象方法繼承實體類

例。射彈

class Projectile : Entity 
 
{ 
 
    Entity Owner; 
 
    ProjectileType pType; 
 

 
    public Projectile(Texture2D image, float maxSpeed, Entity owner, float dmg, ProjectileType type) 
 
    { 
 
     Image = image; 
 
     MaxSpeed = maxSpeed; 
 
     AccelerationSpeed = MaxSpeed; 
 
     Owner = owner; 
 
     Dmg = dmg; 
 
     pType = type; 
 
    } 
 

 
    public override void OnCollision(Collision collision) 
 
    { 
 
     #region If this projectile friendly 
 
     if (pType == ProjectileType.Friendly) 
 
     { 
 
      //If colliding with an enemy 
 
      if (collision.Other.GetType() == typeof(Enemy)) 
 
      { 
 
       var enemy = (Enemy)collision.Other; 
 
       enemy.Health -= Dmg; 
 
       Destroy(this); 
 
      } 
 
     } 
 
     #endregion 
 

 
     #region If this projectile is hostile 
 
     if (pType == ProjectileType.Hostile) 
 
     { 
 

 
     } 
 
     #endregion 
 
    } 
 
}

然後我想從我的遊戲對象類更新調用OnCollision方法。 這是我試圖告訴我的對象,如果他們collding,他們正在與碰撞誰:

 if (GetType().IsAssignableFrom(typeof(Entity))) 
 
     { 
 
      Entity entity = (Entity)this; 
 

 
      if (GetType() == typeof(Player)) 
 
       entity = (Player)this; 
 
      if (GetType() == typeof(Enemy)) 
 
       entity = (Enemy)this; 
 
      if (GetType() == typeof(Projectile)) 
 
       entity = (Projectile)this; 
 

 
      var entityCol = FindCollision(entity); 
 

 
      if (entityCol != null) 
 
       entity.OnCollision(entityCol); 
 
     }

我是新來的抽象類&壓倒一切,所以我可能已經得到了整個想法都是錯誤的。 但它似乎我試過的Debug.WriteLine的東西,但沒有在輸出窗口顯示出來沒有達到OnCollision方法。

感謝您閱讀&也許是想幫我出:)

Mediafire link下載項目要查看所有代碼的情況下。

+1

這可能是entityCol爲空,所以沒有嘗試調用OnCollision方法? – PaulF

+0

我只是試着調試值,它似乎甚至沒有通過'if(GetType()。IsAssignableFrom(typeof(Entity)))''。玩家,實體和子彈繼承了實體類......那麼這不正確? –

+0

嗨。只需要1分:您在UpdateCollisions中的嵌套循環不需要從0開始。它可以從+1開始。在碰撞事件沒有發生的情況下,您需要在調用UpdateCollisions之後建立AllCollisions所具有的功能。該容器中的數據應該是什麼? FindCollision是否返回正確的值?你需要把它分開一點點,直到你找到斷開的鏈接。不是一個答案,但我希望我已經把你推向了正確的方向。 –

回答

1

呃,不對if語句...

使用

if (GetType().IsSubclassOf(typeof(Entity))) 

固定它。

愚蠢的錯誤,是我不好。

+0

我認爲那個「GetType()== typeof」if語句裏面的內容也是多餘的,因爲你已經把它分配給了實體。 – PaulF

+0

是的,你說得對,我在那裏想着什麼...... –

2

您應該interfaces閱讀起來。接口提供派生類必須實現的約定(一堆方法和屬性)。抽象類比接口更具體,因爲它們也可以提供派生類的基本實現。您只能從一個抽象類派生,而您可以從多個接口派生。從你的文章中的代碼看起來你正在使用像接口這樣的抽象類。

您正在使用反射來進行類型檢查。有is關鍵字用於測試類型兼容性。例如:

if(entity is Player) 
{ 
    var player = (Player)entity; 

    // player specific code 
} 

最後;從我可以從你的帖子中收集的內容看起來你並沒有正確地使用繼承。看起來您正確地使用繼承來構建類型層次結構,然後將所有邏輯放在基類中。

繼承是爲了讓你把專業邏輯放在適當的類中。

public interface IGameObject 
{ 
    void OnCollision(IGameObject target); 
} 

public class Player : IGameObject 
{ 
    public void OnCollision(IGameObject target) 
    { 
     Console.WriteLine("Player collision"); 
    } 
} 

public class Projectile : IGameObject 
{ 
    public void OnCollision(IGameObject target) 
    { 
     Console.WriteLine("Projectile collision"); 
    } 
} 

當我們再有一個IGameObject參考,並調用OnCollision適當OnCollision功能將自動被調用。例如:

IGameObject player = new Player(); 
IGameObject projectile = new Projectile(); 

player.OnCollision(projectile); 
projectile.OnCollision(player); 

輸出:

Player collision 
Projectile collision 
+0

哦,哇,謝謝,我會考慮這個。我將盡我所能去做這件事,因爲這個星期五的任務需要完成。 –