private void Initialize()
        {
            Canvas = new Sprite();

            //s.bitmapData.setPixel(1, 1, ColorGreen);
            Canvas.scaleX = DefaultZoom;
            Canvas.scaleY = DefaultZoom;

            Canvas.graphics.beginFill(ColorBlack);
            Canvas.graphics.drawRect(0, 0, RoomWidth, RoomHeight);


            Canvas.AttachTo(this);

            // add scull ani here
            // add status text here

            //var Status = new TextField { autoSize = TextFieldAutoSize.LEFT, textColor = 0xffffff }.AttachTo(this);

            Func <Point> GetRandomLocation =
                () => new Point
            {
                x = (RoomWidth - 1).Random(),
                y = (RoomHeight - 1).Random()
            };



            Func <Apple> CreateApple =
                () => new Apple
            {
                GetRandomLocation = GetRandomLocation,
                Wrapper           = Wrapper
            }.MoveToRandomLocation();


            AddApples = delegate
            {
                20.Times(() =>
                         CreateApple().AttachTo(Canvas).AddTo(Apples)
                         );
            };

            AddApples();

            Ego = new Worm
            {
                Wrapper  = Wrapper,
                Location = GetRandomLocation(),
                Canvas   = Canvas,
                Vector   = Worm.VectorRight
                           // Color = game_colors.worm.active
            }
            .AddTo(Worms)
            .Grow();
            //.GrowToVector()
            //.GrowToVector();



            #region keyboard

            MovementWASD = new KeyboardButtonGroup {
                Name = "WASD"
            };
            MovementArrows = new KeyboardButtonGroup {
                Name = "Arrows"
            };

            var GoLeft = new KeyboardButton(stage)
            {
                Groups = new[]
                {
                    MovementWASD[Keyboard.A],
                    MovementArrows[Keyboard.LEFT],
                },
                Up = () => { Ego.Vector = Worm.VectorLeft; },
            }.Up;

            var GoRight = new KeyboardButton(stage)
            {
                Groups = new[]
                {
                    MovementWASD[Keyboard.D],
                    MovementArrows[Keyboard.RIGHT],
                },
                Up = () => { Ego.Vector = Worm.VectorRight; },
            }.Up;

            var GoUp = new KeyboardButton(stage)
            {
                Groups = new[]
                {
                    MovementWASD[Keyboard.W],
                    MovementArrows[Keyboard.UP],
                },
                Up = () => { Ego.Vector = Worm.VectorUp; },
            }.Up;

            var GoDown = new KeyboardButton(stage)
            {
                Groups = new[]
                {
                    MovementWASD[Keyboard.S],
                    MovementArrows[Keyboard.DOWN],
                },
                Up = () => { Ego.Vector = Worm.VectorDown; },
            }.Up;

            #endregion

            Ego.VectorChanged +=
                delegate
            {
                Sounds.flag.ToSoundAsset().play();
            };
            //var info = new TextField
            //{
            //    textColor = 0xffffff,
            //    autoSize = TextFieldAutoSize.LEFT,
            //}.AttachTo(this);

            Action <MouseEvent> Go =

                e =>
            {
                #region Diaww
                var x = e.stageX;
                var y = e.stageY;


                var A = x > y;
                var B = x > (stage.height - y);

                var DiaClipLeft   = !A && !B;
                var DiaClipRight  = A && B;
                var DiaClipTop    = A && !B;
                var DiaClipBottom = !A && B;
                #endregion

                //info.text = new { e.stageX, stage.width, e.stageY }.ToString();

                if (DiaClipLeft)
                {
                    GoLeft();
                }
                else if (DiaClipRight)
                {
                    GoRight();
                }
                else if (DiaClipTop)
                {
                    GoUp();
                }
                else if (DiaClipBottom)
                {
                    GoDown();
                }
            };

            stage.click     += Go;
            stage.mouseMove +=
                e =>
            {
                if (e.buttonDown)
                {
                    Go(e);
                }
            };


            Sounds.reveal.ToSoundAsset().play();

            (1000 / 20).AtInterval(
                t =>
            {
                foreach (var p in Worms)
                {
                    var worm = p;

                    if (Apples.Count > 0)
                    {
                        if (worm.IsAlive)
                        {
                            //Status.text = "";


                            worm.GrowToVector();

                            if (worm.ThisNetworkInstanceCannotEat)
                            {
                                worm.Shrink();
                            }
                            else
                            {
                                // is there a worm smaller than us that we can eat?
                                if (worm.Parts.Count > 1)
                                {
                                    var OtherWorms = Worms.Where(k => k != worm).ToArray();



                                    //Status.appendText("others: " + OtherWorms.Length);

                                    var ToBeEaten = OtherWorms.FirstOrDefault(k => k.Parts.Any(i => i.Location.IsEqual(worm.NextLocation) || i.Location.IsEqual(worm.Location)));

                                    if (ToBeEaten != null)
                                    {
                                        if (ToBeEaten.IsAlive)
                                        {
                                            if (ToBeEaten.Parts.Count < worm.Parts.Count)
                                            {
                                                ToBeEaten.Color = 0x8f8f8f;

                                                worm.EatThisWormSoon(ToBeEaten);
                                            }
                                            else
                                            {
                                                //Status.appendText(" target not smaller");
                                            }
                                        }
                                        else
                                        {
                                            //Status.appendText(" target not alive");
                                        }
                                    }
                                    else
                                    {
                                        //Status.appendText(" noone in range");
                                    }
                                }

                                // did we find an apple?
                                var a = Apples.Where(i => i.Location.IsEqual(worm.Location)).ToArray();

                                if (a.Length > 0)
                                {
                                    foreach (var v in a)
                                    {
                                        v.Control.Orphanize();
                                        Apples.Remove(v);

                                        if (worm.HasEatenAnApple != null)
                                        {
                                            worm.HasEatenAnApple(v);
                                        }
                                    }

                                    Sounds.sound20.ToSoundAsset().play();
                                    // AddScore(1);
                                }
                                else
                                {
                                    worm.Shrink();
                                }
                            }
                        }
                    }
                }
            }
                );
        }
示例#2
0
		private void InitializeEvents()
		{
			Events.ServerPlayerHello +=
				e =>
				{
					MyIdentity = e;

					ShowMessage("Howdy, " + e.name);
				};


			Action<int, string> CreateRemoteEgo =
				(user, name) =>
				{
					// could use event instead
					// dictonary.valueset +=
					CoPlayerNames[user] = name;

					// create remote ego
					RemoteEgos[user] = new Worm
					{
						Color = 0xff,
						Canvas = Map.Canvas,
						Wrapper = Map.Wrapper,
						Location = new Point(),
						ThisNetworkInstanceCannotEat = true
					}.AddTo(Map.Worms).Grow();

					//ShowMessage("remote worm created - " + Map.Worms.Count);
				};

			Events.ServerPlayerJoined +=
			  e =>
			  {
				  CreateRemoteEgo(e.user, e.name);


				  ShowMessage("Player joined - " + e.name);

				  Messages.PlayerAdvertise(MyIdentity.name);
				  Messages.TeleportTo((int)Map.Ego.Location.x, (int)Map.Ego.Location.y);

				  for (int i = 1; i < Map.Ego.Parts.Count; i++)
				  {
					  // there is no apple, but we just need to gain in size

					  Messages.EatApple((int)Map.Ego.Location.x, (int)Map.Ego.Location.y);
				  }
			  };



			#region ServerPlayerLeft
			Events.ServerPlayerLeft +=
				  e =>
				  {
					  if (CoPlayerNames.ContainsKey(e.user))
					  {
						  CoPlayerNames.Remove(e.user);
					  }

					  if (Cursors.ContainsKey(e.user))
					  {
						  Cursors[e.user].Orphanize();
						  Cursors.Remove(e.user);
					  }

					  if (RemoteEgos.ContainsKey(e.user))
					  {
						  var w = RemoteEgos[e.user];

						  while (w.Parts.Count > 0)
							  w.Shrink();

						  Map.Worms.Remove(w);
						  RemoteEgos.Remove(e.user);
					  }

					  ShowMessage("Player left - " + e.name);
				  };
			#endregion

			Events.UserPlayerAdvertise +=
				e =>
				{
					if (CoPlayerNames.ContainsKey(e.user))
						return;

					CreateRemoteEgo(e.user, e.name);

					// ShowMessage("Player already here - " + e.name);
				};


			#region MouseMove
			Events.UserMouseMove +=
					e =>
					{
						var s = default(ShapeWithMovement);

						if (Cursors.ContainsKey(e.user))
							s = Cursors[e.user];
						else
						{
							s = new ShapeWithMovement
							{
								filters = new[] { new DropShadowFilter() },
								alpha = 0.5
							};


							var g = s.graphics;

							g.beginFill((uint)e.color);
							g.moveTo(0, 0);
							g.lineTo(14, 14);
							g.lineTo(0, 20);
							g.lineTo(0, 0);
							g.endFill();

							Cursors[e.user] = s;
						};

						s.AttachTo(this).MoveTo(e.x, e.y);
					};
			

			Events.UserMouseOut +=
			   e =>
			   {
				   if (Cursors.ContainsKey(e.color))
				   {
					   Cursors[e.color].Orphanize();
				   }
			   };
			#endregion

			Events.UserVectorChanged +=
				e =>
				{
					if (RemoteEgos.ContainsKey(e.user))
					{
						RemoteEgos[e.user].Vector = new Point(e.x, e.y);
					}
				};

			Events.ServerSendMap +=
				e =>
				{
					
					OnServerSendMap();

				};

			Events.UserLevelHasEnded +=
				e =>
				{
					// update rankings

					OnLevelHasEnded();
				};

			Events.UserSendMap +=
				e =>
				{
					// we got a new map, do we need it?

					foreach (var v in Map.Apples)
					{
						v.Control.Orphanize();
					}

					Map.Apples.Clear();

					// now add apples as the new map says
					var integers_as_bytes = e.buttons.Select(i => (byte)i).ToArray();

					var m = new MemoryStream(integers_as_bytes);

					var AppleCount = m.ReadByte();

					for (int i = 0; i < AppleCount; i++)
					{
						new Apple
						{
							Wrapper = Map.Wrapper,
							Location = new Point(m.ReadByte(), m.ReadByte())
						}.AddTo(Map.Apples).AttachTo(Map.Canvas);
					}

					//ShowMessage("got map: " + integers_as_bytes.Length);
				};

			Events.UserTeleportTo +=
				e =>
				{
					//ShowMessage(new { teleport = e.user, e.x, e.y }.ToString());

					if (RemoteEgos.ContainsKey(e.user))
					{
						var w = RemoteEgos[e.user];

						w.Color = 0xff;
						w.IsAlive = true;
						w.Location = new Point(e.x, e.y);
						w.Grow();
						w.Shrink();
					}
				};

			Events.UserEatApple +=
				e =>
				{
					if (RemoteEgos.ContainsKey(e.user))
					{
						var w = RemoteEgos[e.user];

						w.Grow();


						foreach (var v in from i in Map.Apples
										  where i.Location.IsEqual(new Point(e.x, e.y))
										  select i)
						{
							v.Control.Orphanize();
							Map.Apples.Remove(v);

							Sounds.sound20.ToSoundAsset().play();

						}

					}
				};

			#region EatThisWorm
			Events.UserEatThisWormBegin +=
				e =>
				{
					if (!RemoteEgos.ContainsKey(e.user))
						return;

					var user = RemoteEgos[e.user];

					if (!AllEgos.Any(i => i.Key == e.food))
						return;

					var food = AllEgos.Single(i => i.Key == e.food).Value;

					
					food.WormWhoIsGoingToEatMe = user;
					food.Color = 0x8f8f8f;

					// whatif async end never comes?

				};

			Events.UserEatThisWormEnd +=
				e =>
				{
					if (!RemoteEgos.ContainsKey(e.user))
						return;

					var user = RemoteEgos[e.user];

					if (!AllEgos.Any(i => i.Key == e.food))
						return;

					var food = AllEgos.Single(i => i.Key == e.food).Value;

					food.IsAlive = false;

					// do the eating 
					while (food.Parts.Count > 1)
					{
						food.Shrink();
						user.Grow();
					}

					food.WormWhoIsGoingToEatMe = null;

					if (food == Map.Ego)
					{
						// we now decide how long must we be dead

						DeathDelay.AtDelayDo(
							delegate
							{
								food.IsAlive = true;
								food.Color = 0x00ff00;

								Messages.TeleportTo((int)Map.Ego.Location.x, (int)Map.Ego.Location.y);
							}
						);
					}

				};
			#endregion

		}
		private void Initialize()
		{
			Canvas = new Sprite();

			//s.bitmapData.setPixel(1, 1, ColorGreen);
			Canvas.scaleX = DefaultZoom;
			Canvas.scaleY = DefaultZoom;

			Canvas.graphics.beginFill(ColorBlack);
			Canvas.graphics.drawRect(0, 0, RoomWidth, RoomHeight);


			Canvas.AttachTo(this);

			// add scull ani here
			// add status text here

			//var Status = new TextField { autoSize = TextFieldAutoSize.LEFT, textColor = 0xffffff }.AttachTo(this);

			Func<Point> GetRandomLocation =
				() => new Point
				{
					x = (RoomWidth - 1).Random(),
					y = (RoomHeight - 1).Random()
				};



			Func<Apple> CreateApple =
				   () => new Apple
				   {
					   GetRandomLocation = GetRandomLocation,
					   Wrapper = Wrapper
				   }.MoveToRandomLocation();


			AddApples = delegate
			{
				20.Times(() =>
					CreateApple().AttachTo(Canvas).AddTo(Apples)
				);
			};

			AddApples();

			Ego = new Worm
			{
				Wrapper = Wrapper,
				Location = GetRandomLocation(),
				Canvas = Canvas,
				Vector = Worm.VectorRight
				// Color = game_colors.worm.active
			}
			.AddTo(Worms)
			 .Grow();
			//.GrowToVector()
			//.GrowToVector();



			#region keyboard

			MovementWASD = new KeyboardButtonGroup { Name = "WASD" };
			MovementArrows = new KeyboardButtonGroup { Name = "Arrows" };

			var GoLeft = new KeyboardButton(stage)
			{
				Groups = new[]
                {
                    MovementWASD[Keyboard.A],
                    MovementArrows[Keyboard.LEFT],
                },
				Up = () => { Ego.Vector = Worm.VectorLeft; },
			}.Up;

			var GoRight = new KeyboardButton(stage)
			{
				Groups = new[]
                {
                    MovementWASD[Keyboard.D],
                    MovementArrows[Keyboard.RIGHT],
                },
				Up = () => { Ego.Vector = Worm.VectorRight; },
			}.Up;

			var GoUp = new KeyboardButton(stage)
			{
				Groups = new[]
                {
                    MovementWASD[Keyboard.W],
                    MovementArrows[Keyboard.UP],
                },
				Up = () => { Ego.Vector = Worm.VectorUp; },
			}.Up;

			var GoDown = new KeyboardButton(stage)
			{
				Groups = new[]
                {
                    MovementWASD[Keyboard.S],
                    MovementArrows[Keyboard.DOWN],
                },
				Up = () => { Ego.Vector = Worm.VectorDown; },

			}.Up;

			#endregion

			Ego.VectorChanged +=
				delegate
				{
					Sounds.flag.ToSoundAsset().play();
				};
			//var info = new TextField
			//{
			//    textColor = 0xffffff,
			//    autoSize = TextFieldAutoSize.LEFT,
			//}.AttachTo(this);

			Action<MouseEvent> Go =

				e =>
				{
					#region Diaww
					var x = e.stageX;
					var y = e.stageY;


					var A = x > y;
					var B = x > (stage.height - y);

					var DiaClipLeft = !A && !B;
					var DiaClipRight = A && B;
					var DiaClipTop = A && !B;
					var DiaClipBottom = !A && B;
					#endregion

					//info.text = new { e.stageX, stage.width, e.stageY }.ToString();

					if (DiaClipLeft) GoLeft();
					else if (DiaClipRight) GoRight();
					else if (DiaClipTop) GoUp();
					else if (DiaClipBottom) GoDown();
				};

			stage.click += Go;
			stage.mouseMove +=
				e =>
				{
					if (e.buttonDown) Go(e);
				};


			Sounds.reveal.ToSoundAsset().play();

			(1000 / 20).AtInterval(
			   t =>
			   {
				   foreach (var p in Worms)
				   {
					   var worm = p;

					   if (Apples.Count > 0)
						   if (worm.IsAlive)
						   {

							   //Status.text = "";


							   worm.GrowToVector();

							   if (worm.ThisNetworkInstanceCannotEat)
							   {
								   worm.Shrink();
							   }
							   else
							   {
								   // is there a worm smaller than us that we can eat?
								   if (worm.Parts.Count > 1)
								   {
									   var OtherWorms = Worms.Where(k => k != worm).ToArray();



									   //Status.appendText("others: " + OtherWorms.Length);

									   var ToBeEaten = OtherWorms.FirstOrDefault(k => k.Parts.Any(i => i.Location.IsEqual(worm.NextLocation) || i.Location.IsEqual(worm.Location)));

									   if (ToBeEaten != null)
									   {


										   if (ToBeEaten.IsAlive)
										   {
											   if (ToBeEaten.Parts.Count < worm.Parts.Count)
											   {
												   ToBeEaten.Color = 0x8f8f8f;

												   worm.EatThisWormSoon(ToBeEaten);
											   }
											   else
											   {
												   //Status.appendText(" target not smaller");
											   }
										   }
										   else
										   {
											   //Status.appendText(" target not alive");
										   }


									   }
									   else
									   {
										   //Status.appendText(" noone in range");
									   }
								   }

								   // did we find an apple?
								   var a = Apples.Where(i => i.Location.IsEqual(worm.Location)).ToArray();

								   if (a.Length > 0)
								   {
									   foreach (var v in a)
									   {
										   v.Control.Orphanize();
										   Apples.Remove(v);

										   if (worm.HasEatenAnApple != null)
											   worm.HasEatenAnApple(v);
									   }

									   Sounds.sound20.ToSoundAsset().play();
									   // AddScore(1);
								   }
								   else
								   {
									   worm.Shrink();
								   }
							   }
						   }
				   }



			   }
			);
		}