Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Timers;
- using CryEngine2;
- using CryEngine2.API;
- using Snake.Entities;
- using System.Threading;
- namespace Snake
- {
- public class SnakeGame : BaseGame
- {
- private const float SnakeFieldHeight = 40; // Constant which defines at which height we're playing
- private System.Timers.Timer _snakeTimer;
- public Food TheFood { get; set; }
- public ComposedSnake ComposedSnake;
- public SnakeGame()
- {
- // Attach some eventhandlers
- GameStarted += SnakeGameGameStarted;
- Reset += SnakeGameReset;
- Input += SnakeGameInput;
- }
- void SnakeGameInput(object sender, InputEventArgs e)
- {
- if (e.KeyId == EKeyId.KI_Left)
- ComposedSnake.Direction = ESnakeDirection.Left;
- else if (e.KeyId == EKeyId.KI_Up)
- ComposedSnake.Direction = ESnakeDirection.Up;
- else if (e.KeyId == EKeyId.KI_Right)
- ComposedSnake.Direction = ESnakeDirection.Right;
- else if (e.KeyId == EKeyId.KI_Down)
- ComposedSnake.Direction = ESnakeDirection.Down;
- }
- private void SnakeGameReset(object sender, EntityResetEventArgs e)
- {
- // Check if we're going out of the game (game->editor) so we can stop our timer to move the snake
- if (!e.Ingame)
- {
- if (_snakeTimer != null)
- _snakeTimer.Stop();
- ComposedSnake = null;
- }
- }
- private void SnakeGameGameStarted(object sender, EventArgs e)
- {
- // Workaround
- Thread t = new Thread(ts =>
- {
- Thread.Sleep(1);
- ComposedSnake = new ComposedSnake { Direction = ESnakeDirection.Right };
- Console.WriteLine("Spawning");
- ComposedSnake.Spawn(5);
- Console.WriteLine("Spawned");
- TheFood = EntitySystem.SpawnEntity<Food>();
- TheFood.Position = GetFoodPosition();
- // Launch the snake timer
- _snakeTimer = new System.Timers.Timer(50);
- _snakeTimer.Elapsed += SnakeTimerElapsed;
- _snakeTimer.Start();
- });
- t.Start();
- }
- private void SnakeTimerElapsed(object sender, ElapsedEventArgs e)
- {
- if (ComposedSnake != null && TheFood != null)
- {
- ComposedSnake.UpdatePositions();
- Vec3 posSnake = ComposedSnake.SnakeHead.Position;
- Vec3 posFood = TheFood.Position;
- if (posSnake.X == posFood.X && posSnake.Y == posFood.Y)
- {
- ComposedSnake.Enlarge();
- TheFood.Position = GetFoodPosition();
- }
- }
- }
- readonly Random _random = new Random();
- private Vec3 GetFoodPosition()
- {
- // Get a random food position on the grid
- Vec3 position = new Vec3 { X = _random.Next(30, 60), Y = _random.Next(30, 60), Z = SnakeFieldHeight };
- return position;
- }
- }
- }
- using System;
- using System.Collections.Generic;
- using Snake.Entities;
- using CryEngine2.API;
- using CryEngine2;
- namespace Snake
- {
- public class ComposedSnake
- {
- public List<SnakePart> Parts { get; set; }
- public int Length { get; set; }
- public ESnakeDirection Direction { get; set; }
- bool _enlarging;
- public SnakePart SnakeHead
- {
- get
- {
- return Parts[0];
- }
- }
- public ComposedSnake()
- {
- Console.WriteLine("Composing the snake");
- }
- public void Spawn(int length)
- {
- Length = length;
- SnakePart previousSnakePart = null;
- Vec3 position = new Vec3(50, 50, 40);
- Parts = new List<SnakePart>();
- for (int i = 0; i < Length; i++)
- {
- Console.WriteLine("Spawning " + i + " on " + position);
- SnakePart part = EntitySystem.SpawnEntity<SnakePart>();
- if (part == null)
- Console.WriteLine("Failed to spawn entity");
- else
- {
- part.PreviousSnakePart = previousSnakePart;
- part.Position = new Vec3(50 - i, 50, 40);
- Parts.Add(part);
- previousSnakePart = part;
- }
- }
- //EntitySystem.SpawnEntity<SnakePart>();
- }
- public void Enlarge()
- {
- Console.WriteLine("Enlarge!");
- _enlarging = true;
- }
- public void UpdatePositions()
- {
- Vec3 tailPos = Parts[Parts.Count - 1].Position;
- for (int i = Parts.Count - 1; i > 0; i--)
- {
- Vec3 moveTo = Parts[i - 1].Position;
- Parts[i].Position = moveTo;
- }
- Vec3 foo = Parts[0].Position;
- switch (Direction)
- {
- case ESnakeDirection.Left:
- foo.X -= 1;
- break;
- case ESnakeDirection.Up:
- foo.Y += 1;
- break;
- case ESnakeDirection.Right:
- foo.X += 1;
- break;
- case ESnakeDirection.Down:
- foo.Y -= 1;
- break;
- default:
- break;
- }
- SnakeHead.Position = foo;
- if (_enlarging)
- {
- SnakePart p = EntitySystem.SpawnEntity<SnakePart>();
- p.Position = tailPos;
- Parts.Add(p);
- _enlarging = false;
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement