[gnome-nibbles/wip/vala: 9/16] Fix head to head collision
- From: Michael Catanzaro <mcatanzaro src gnome org>
- To: commits-list gnome org
- Cc:
- Subject: [gnome-nibbles/wip/vala: 9/16] Fix head to head collision
- Date: Sat, 27 Jun 2015 17:03:24 +0000 (UTC)
commit d5e727f63781d05833d188cb2d21cdbbb7eca88b
Author: Iulian Radu <iulian radu67 gmail com>
Date: Fri Jun 26 21:56:33 2015 +0300
Fix head to head collision
src/nibbles-game.vala | 16 +++++++++++++---
src/nibbles-view.vala | 4 ++--
src/worm.vala | 10 ++++++++++
3 files changed, 25 insertions(+), 5 deletions(-)
---
diff --git a/src/nibbles-game.vala b/src/nibbles-game.vala
index 81c907f..d919bad 100644
--- a/src/nibbles-game.vala
+++ b/src/nibbles-game.vala
@@ -10,7 +10,7 @@ public class NibblesGame : Object
public const int NETDELAY = 2;
public const int BONUSDELAY = 100;
- public const int NUMWORMS = 1;
+ public const int NUMWORMS = 2;
public const int WIDTH = 92;
public const int HEIGHT = 66;
@@ -61,8 +61,18 @@ public class NibblesGame : Object
{
if (worm.stop)
continue;
- if (!worm.can_move_to (walls, numworms)) {
- stderr.printf("[Debug] died\n");
+
+ foreach (var other_worm in worms)
+ if (worm != other_worm
+ && worm.collides_with_head (other_worm.head ()))
+ {
+ worm.die (walls);
+ other_worm.die (walls);
+ continue;
+ }
+
+ if (!worm.can_move_to (walls, numworms))
+ {
worm.die (walls);
continue;
}
diff --git a/src/nibbles-view.vala b/src/nibbles-view.vala
index f451570..06d6c46 100644
--- a/src/nibbles-view.vala
+++ b/src/nibbles-view.vala
@@ -121,7 +121,7 @@ public class NibblesView : GtkClutter.Embed
game.walls[j, i] = NibblesGame.EMPTYCHAR;
if (count < game.numworms)
{
- var worm = new Worm (count++, WormDirection.DOWN);
+ var worm = new Worm (count++, WormDirection.LEFT);
worm.added.connect (worm_added_cb);
worm.moved.connect (worm_moved_cb);
worm.rescaled.connect (worm_rescaled_cb);
@@ -137,7 +137,7 @@ public class NibblesView : GtkClutter.Embed
game.walls[j, i] = NibblesGame.EMPTYCHAR;
if (count < game.numworms)
{
- var worm = new Worm (count++, WormDirection.LEFT);
+ var worm = new Worm (count++, WormDirection.DOWN);
worm.added.connect (worm_added_cb);
worm.moved.connect (worm_moved_cb);
worm.rescaled.connect (worm_rescaled_cb);
diff --git a/src/worm.vala b/src/worm.vala
index 453406d..8872a9d 100644
--- a/src/worm.vala
+++ b/src/worm.vala
@@ -152,6 +152,16 @@ public class Worm : Object
return true;
}
+ public bool collides_with_head (Position other_head)
+ {
+ if (head ().x == other_head.x)
+ return head ().y - 1 == other_head.y || head ().y + 1 == other_head.y;
+ if (head ().y == other_head.y)
+ return head ().x - 1 == other_head.x || head ().x + 1 == other_head.x;
+
+ return false;
+ }
+
public void spawn (int[,] walls)
{
for (int i = 0; i < STARTING_LENGTH; i++)
[
Date Prev][
Date Next] [
Thread Prev][
Thread Next]
[
Thread Index]
[
Date Index]
[
Author Index]