Advertisement
Guest User

Untitled

a guest
Jun 24th, 2017
61
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 6.04 KB | None | 0 0
  1. using System;
  2. using System.Timers;
  3. using CryEngine2;
  4. using CryEngine2.API;
  5. using Snake.Entities;
  6. using System.Threading;
  7.  
  8. namespace Snake
  9. {
  10.     public class SnakeGame : BaseGame
  11.     {
  12.         private const float SnakeFieldHeight = 40; // Constant which defines at which height we're playing
  13.         private System.Timers.Timer _snakeTimer;
  14.         public Food TheFood { get; set; }
  15.         public ComposedSnake ComposedSnake;
  16.  
  17.         public SnakeGame()
  18.         {
  19.             // Attach some eventhandlers
  20.             GameStarted += SnakeGameGameStarted;
  21.             Reset += SnakeGameReset;
  22.             Input += SnakeGameInput;
  23.         }
  24.  
  25.         void SnakeGameInput(object sender, InputEventArgs e)
  26.         {
  27.             if (e.KeyId == EKeyId.KI_Left)
  28.                 ComposedSnake.Direction = ESnakeDirection.Left;
  29.             else if (e.KeyId == EKeyId.KI_Up)
  30.                 ComposedSnake.Direction = ESnakeDirection.Up;
  31.             else if (e.KeyId == EKeyId.KI_Right)
  32.                 ComposedSnake.Direction = ESnakeDirection.Right;
  33.             else if (e.KeyId == EKeyId.KI_Down)
  34.                 ComposedSnake.Direction = ESnakeDirection.Down;
  35.         }
  36.  
  37.         private void SnakeGameReset(object sender, EntityResetEventArgs e)
  38.         {
  39.             // Check if we're going out of the game (game->editor) so we can stop our timer to move the snake
  40.             if (!e.Ingame)
  41.             {
  42.                 if (_snakeTimer != null)
  43.                     _snakeTimer.Stop();
  44.  
  45.                 ComposedSnake = null;
  46.             }
  47.         }
  48.  
  49.         private void SnakeGameGameStarted(object sender, EventArgs e)
  50.         {
  51.             // Workaround
  52.             Thread t = new Thread(ts =>
  53.                 {
  54.                     Thread.Sleep(1);
  55.  
  56.                     ComposedSnake = new ComposedSnake { Direction = ESnakeDirection.Right };
  57.  
  58.                     Console.WriteLine("Spawning");
  59.                     ComposedSnake.Spawn(5);
  60.                     Console.WriteLine("Spawned");
  61.  
  62.  
  63.                     TheFood = EntitySystem.SpawnEntity<Food>();
  64.  
  65.                     TheFood.Position = GetFoodPosition();
  66.  
  67.                     // Launch the snake timer
  68.                     _snakeTimer = new System.Timers.Timer(50);
  69.                     _snakeTimer.Elapsed += SnakeTimerElapsed;
  70.                     _snakeTimer.Start();
  71.                 });
  72.  
  73.             t.Start();
  74.         }
  75.  
  76.         private void SnakeTimerElapsed(object sender, ElapsedEventArgs e)
  77.         {
  78.             if (ComposedSnake != null && TheFood != null)
  79.             {
  80.                 ComposedSnake.UpdatePositions();
  81.  
  82.                 Vec3 posSnake = ComposedSnake.SnakeHead.Position;
  83.                 Vec3 posFood = TheFood.Position;
  84.  
  85.                 if (posSnake.X == posFood.X && posSnake.Y == posFood.Y)
  86.                 {
  87.                     ComposedSnake.Enlarge();
  88.                     TheFood.Position = GetFoodPosition();
  89.                 }
  90.             }
  91.         }
  92.  
  93.         readonly Random _random = new Random();
  94.  
  95.         private Vec3 GetFoodPosition()
  96.         {
  97.             // Get a random food position on the grid
  98.             Vec3 position = new Vec3 { X = _random.Next(30, 60), Y = _random.Next(30, 60), Z = SnakeFieldHeight };
  99.  
  100.             return position;
  101.         }
  102.     }
  103. }
  104.  
  105.  
  106.  
  107.  
  108.  
  109.  
  110.  
  111. using System;
  112. using System.Collections.Generic;
  113. using Snake.Entities;
  114. using CryEngine2.API;
  115. using CryEngine2;
  116.  
  117. namespace Snake
  118. {
  119.     public class ComposedSnake
  120.     {
  121.         public List<SnakePart> Parts { get; set; }
  122.         public int Length { get; set; }
  123.         public ESnakeDirection Direction { get; set; }
  124.         bool _enlarging;
  125.  
  126.         public SnakePart SnakeHead
  127.         {
  128.             get
  129.             {
  130.                 return Parts[0];
  131.             }
  132.         }
  133.  
  134.         public ComposedSnake()
  135.         {
  136.             Console.WriteLine("Composing the snake");
  137.         }
  138.  
  139.         public void Spawn(int length)
  140.         {
  141.             Length = length;
  142.  
  143.             SnakePart previousSnakePart = null;
  144.             Vec3 position = new Vec3(50, 50, 40);
  145.             Parts = new List<SnakePart>();
  146.  
  147.  
  148.             for (int i = 0; i < Length; i++)
  149.             {
  150.                 Console.WriteLine("Spawning " + i + " on " + position);
  151.                 SnakePart part = EntitySystem.SpawnEntity<SnakePart>();
  152.  
  153.                 if (part == null)
  154.                     Console.WriteLine("Failed to spawn entity");
  155.                 else
  156.                 {
  157.                     part.PreviousSnakePart = previousSnakePart;
  158.                     part.Position = new Vec3(50 - i, 50, 40);
  159.  
  160.  
  161.  
  162.                     Parts.Add(part);
  163.                     previousSnakePart = part;
  164.                 }
  165.             }
  166.             //EntitySystem.SpawnEntity<SnakePart>();
  167.         }
  168.  
  169.         public void Enlarge()
  170.         {
  171.             Console.WriteLine("Enlarge!");
  172.             _enlarging = true;
  173.         }
  174.  
  175.         public void UpdatePositions()
  176.         {
  177.             Vec3 tailPos = Parts[Parts.Count - 1].Position;
  178.  
  179.             for (int i = Parts.Count - 1; i > 0; i--)
  180.             {
  181.                 Vec3 moveTo = Parts[i - 1].Position;
  182.  
  183.                 Parts[i].Position = moveTo;
  184.             }
  185.  
  186.             Vec3 foo = Parts[0].Position;
  187.  
  188.             switch (Direction)
  189.             {
  190.                 case ESnakeDirection.Left:
  191.                     foo.X -= 1;
  192.                     break;
  193.                 case ESnakeDirection.Up:
  194.                     foo.Y += 1;
  195.                     break;
  196.                 case ESnakeDirection.Right:
  197.                     foo.X += 1;
  198.                     break;
  199.                 case ESnakeDirection.Down:
  200.                     foo.Y -= 1;
  201.                     break;
  202.                 default:
  203.                     break;
  204.             }
  205.  
  206.             SnakeHead.Position = foo;
  207.  
  208.             if (_enlarging)
  209.             {
  210.                 SnakePart p = EntitySystem.SpawnEntity<SnakePart>();
  211.  
  212.                 p.Position = tailPos;
  213.                 Parts.Add(p);
  214.                 _enlarging = false;
  215.             }
  216.  
  217.         }
  218.  
  219.     }
  220. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement