diff options
author | Jakob Stendahl <jakob.stendahl@outlook.com> | 2017-11-12 19:52:46 +0100 |
---|---|---|
committer | Jakob Stendahl <jakob.stendahl@outlook.com> | 2017-11-12 19:52:46 +0100 |
commit | 76d19004e770c360feecd7a0f5c9b1c875d44bf1 (patch) | |
tree | 384ca77f3c0113d0cb1eb11e609fcdb48778edcc | |
parent | 91f2f03605f8fc2e3555e18826662754e1ee91a0 (diff) | |
download | Space-Invaders-CS-Console-76d19004e770c360feecd7a0f5c9b1c875d44bf1.tar.gz Space-Invaders-CS-Console-76d19004e770c360feecd7a0f5c9b1c875d44bf1.zip |
[a] Moved the CollideCheck into a method on the GameObject class
-rw-r--r-- | Space Invaders/GameEngine/GameEngine.cs | 10 | ||||
-rw-r--r-- | Space Invaders/GameObjects.cs | 16 | ||||
-rw-r--r-- | Space Invaders/Program.cs | 5 |
3 files changed, 16 insertions, 15 deletions
diff --git a/Space Invaders/GameEngine/GameEngine.cs b/Space Invaders/GameEngine/GameEngine.cs index 6c5c5b0..bba5838 100644 --- a/Space Invaders/GameEngine/GameEngine.cs +++ b/Space Invaders/GameEngine/GameEngine.cs @@ -132,6 +132,16 @@ namespace GameEngine { // This method is called after the frame is rendered } + public bool CollidingWith(GameObject other) { + // This method checks if 'this' collides with another GameObject + // TODO Make this check if the position contains '\0', if so we are not colliding. This will make hitboxes more natural + int x1 = other.xPos; + int x2 = other.xPos + other.Sprite.GetLength(1); + int y1 = other.yPos; + int y2 = other.yPos + other.Sprite.GetLength(0); + return (x1 < xPos && xPos < x2 && y1 < yPos && yPos < y2); + } + public void ScaleSprite() { // This is called after the Update method, and scales the sprite if it has changed if (RenderedSprite == Sprite) { return; } diff --git a/Space Invaders/GameObjects.cs b/Space Invaders/GameObjects.cs index f7297a1..26533d6 100644 --- a/Space Invaders/GameObjects.cs +++ b/Space Invaders/GameObjects.cs @@ -5,6 +5,10 @@ namespace SpaceInvaders { class GameLogic : GameObject { + public override void Start() { + + } + public override void Update() { if (Input.KeyPressed(ConsoleKey.Escape)) { @@ -44,22 +48,14 @@ namespace SpaceInvaders { GameObject other = Parent.GameObjects[i]; if (other.Tag == "Enemy") { - int x1 = other.xPos; - int x2 = other.xPos + other.Sprite.GetLength(1); - int y1 = other.yPos; - int y2 = other.yPos + other.Sprite.GetLength(0); - if (x1 <= xPos && xPos <= x2 && y1 <= yPos && yPos <= y2) { + if (CollidingWith(other)) { other.isDead = true; isDead = true; } } if (other.Tag == "Obstacle") { - int x1 = other.xPos; - int x2 = other.xPos + other.Sprite.GetLength(1); - int y1 = other.yPos; - int y2 = other.yPos + other.Sprite.GetLength(0); - if (x1 <= xPos && xPos <= x2 && y1 <= yPos && yPos <= y2) { + if (CollidingWith(other)) { isDead = true; } } diff --git a/Space Invaders/Program.cs b/Space Invaders/Program.cs index bf9751d..769bc0c 100644 --- a/Space Invaders/Program.cs +++ b/Space Invaders/Program.cs @@ -37,13 +37,8 @@ namespace SpaceInvaders { player.yPos = 115; player.Scale = 1; GameObjects.Add(player); - - // Init a monster - GameObject monster1 = new Monster(); - GameObjects.Add(monster1); // Init Obstacles - for (int i = 0; i < 6; i++) { GameObject obstacle = new Obstacle(); player.Parent = this; |