aboutsummaryrefslogtreecommitdiff
path: root/Space Invaders/GameObjects.cs
diff options
context:
space:
mode:
Diffstat (limited to 'Space Invaders/GameObjects.cs')
-rw-r--r--Space Invaders/GameObjects.cs159
1 files changed, 145 insertions, 14 deletions
diff --git a/Space Invaders/GameObjects.cs b/Space Invaders/GameObjects.cs
index 26533d6..9f1db1d 100644
--- a/Space Invaders/GameObjects.cs
+++ b/Space Invaders/GameObjects.cs
@@ -6,7 +6,13 @@ namespace SpaceInvaders {
class GameLogic : GameObject {
public override void Start() {
-
+ for (int i = 0; i < 12; i++) {
+ GameObject monster = new Monster();
+ monster.Parent = Parent;
+ monster.xPos = i * 35 + 90;
+ monster.yPos = 20;
+ Parent.GameObjects.Add(monster);
+ }
}
public override void Update() {
@@ -18,9 +24,9 @@ namespace SpaceInvaders {
}
}
- class Bullet : GameObject {
+ class PlayerBullet : GameObject {
- int moveTimer = 10;
+ public int MoveTimer = 10;
public override void Start() {
Tag = "Projectile";
@@ -30,14 +36,14 @@ namespace SpaceInvaders {
public override void Update() {
- moveTimer--;
- if (moveTimer <= 0) {
+ MoveTimer--;
+ if (MoveTimer <= 0) {
yPos = yPos - 1;
- moveTimer = 10;
+ MoveTimer = 10;
}
- if (yPos <= 0) {
+ if (yPos <= 0 || yPos >= Parent.ConsoleHeight-1) {
isDead = true;
}
@@ -56,6 +62,62 @@ namespace SpaceInvaders {
if (other.Tag == "Obstacle") {
if (CollidingWith(other)) {
+ Obstacle obstacle = (Obstacle)other;
+ obstacle.HP = obstacle.HP - 1;
+ isDead = true;
+ }
+ }
+ }
+ }
+
+ }
+
+ class MonsterBullet : GameObject {
+
+ public int MoveTimer = 10;
+
+ public override void Start() {
+ Tag = "Projectile";
+ Scale = 1;
+ Sprite = new char[1, 1] { { '\u2588' } };
+ }
+
+ public override void Update() {
+
+ MoveTimer--;
+ if (MoveTimer <= 0) {
+ yPos = yPos + 1;
+ MoveTimer = 10;
+ }
+
+
+ if (yPos <= 0 || yPos >= Parent.ConsoleHeight - 1) {
+ isDead = true;
+ }
+
+ }
+
+ public override void LateUpdate(Frame thisFrame) {
+ for (int i = 0; i < Parent.GameObjects.Count; i++) {
+ GameObject other = Parent.GameObjects[i];
+
+ if (other.Tag == "Enemy") {
+ if (CollidingWith(other)) {
+ isDead = true;
+ }
+ }
+
+ if (other.Tag == "Player") {
+ if (CollidingWith(other)) {
+ other.isDead = true;
+ isDead = false;
+ }
+ }
+
+ if (other.Tag == "Obstacle") {
+ if (CollidingWith(other)) {
+ Obstacle obstacle = (Obstacle)other;
+ obstacle.HP = obstacle.HP - 1;
isDead = true;
}
}
@@ -88,7 +150,7 @@ namespace SpaceInvaders {
}
if (Input.KeyPressed(ConsoleKey.Spacebar)) {
- GameObject tmpBullet = new Bullet();
+ GameObject tmpBullet = new PlayerBullet();
tmpBullet.Parent = Parent;
tmpBullet.xPos = xPos + 8;
tmpBullet.yPos = yPos - 1;
@@ -101,27 +163,60 @@ namespace SpaceInvaders {
}
class Obstacle : GameObject {
+
+
+ public int HP = 4;
+ private int lastHP = 4;
public override void Start() {
Tag = "Obstacle";
Scale = 4;
- Sprite = new char[2, 5] {
- {'\u2588', '\u2588', '\u2588', '\u2588', '\u2588'},
- {'\u2588', '\0', '\0', '\0', '\u2588'}
- };
+ Sprite = new char[1, 1] { { '\u2588' } };
+ }
+
+ public override void Update() {
+
+ if (lastHP != HP) {
+ lastHP = HP;
+
+ if (HP == 4) {
+ Sprite = new char[1, 1] { { '\u2588' } };
+ }
+ if (HP == 3) {
+ Sprite = new char[1, 1] { { '\u2593' } };
+ }
+ if (HP == 2) {
+ Sprite = new char[1, 1] { { '\u2592' } };
+ }
+ if (HP == 1) {
+ Sprite = new char[1, 1] { { '\u2591' } };
+ }
+ if (HP <= 0) {
+ isDead = true;
+ }
+
+ }
+
}
}
class Monster : GameObject {
+ private int _shootTimer = 400000;
+ private int _moveTimer = 400;
+
+ private bool _movingRight = true;
+ private int _UBound = 20;
+ private int _currStep = 10;
+
public override void Start() {
Tag = "Enemy";
- xPos = 10;
- yPos = 10;
Scale = 1;
+ _shootTimer = Parent.Rand.Next(1500, 3500);
+
Sprite = new char[8, 11] {
{'\0', '\0', '\u2588', '\0', '\0', '\0', '\0', '\0', '\u2588', '\0', '\0'},
{'\0', '\0', '\0', '\u2588', '\0', '\0', '\0', '\u2588', '\0', '\0', '\0'},
@@ -136,6 +231,42 @@ namespace SpaceInvaders {
}
public override void Update() {
+
+ if (_moveTimer <= 0) {
+ _moveTimer = 400;
+ if (_currStep >= _UBound) {
+ _currStep = 0;
+ _movingRight = !_movingRight;
+
+ if (_movingRight) {
+ yPos = yPos + 8;
+ }
+ }
+
+ if (_movingRight) {
+ xPos = xPos + 5;
+ }
+ else {
+ xPos = xPos - 5;
+ }
+
+ _currStep++;
+ }
+ _moveTimer--;
+
+ }
+
+ public override void LateUpdate(Frame thisFrame) {
+ if (_shootTimer <= 0) {
+ _shootTimer = Parent.Rand.Next(1500, 3500); ;
+ MonsterBullet bullet = new MonsterBullet();
+ bullet.Parent = Parent;
+ bullet.xPos = xPos + 10;
+ bullet.yPos = yPos + Sprite.GetLength(0) + 1;
+ bullet.Start();
+ Parent.GameObjects.Add(bullet);
+ }
+ _shootTimer--;
}
}