public SLSnake(SLLevel curLevel) : base(curLevel) { CurLevel = curLevel; Pos = new Point(CurLevel.SnakeStartPosX * CurLevel.TileSize, CurLevel.SnakeStartPosY * CurLevel.TileSize); //Check if the Snake is animated and set necessary animation initialization Animated = CurLevel.AnimatedSnake; if (Animated) { _initSnakeAnimation(); _initCollisionAnimation(); SnakeUtils.InitAnimationInDict(AnimationsDict, "win", CurLevel.Animation.FilenameWin, CurLevel.Animation.WinFrames, CurLevel.LevelCanvas); } else { SnakeImage = SnakeUtils.LoadImage(CurLevel.FilenameHead); CurLevel.LevelCanvas.Children.Add(SnakeImage); } SnakeImage.SetValue(Canvas.LeftProperty, Pos.X); SnakeImage.SetValue(Canvas.TopProperty, Pos.Y + CurLevel.SnakeOffsetY); }
public static void InitAnimationInDict(Dictionary <string, Image[]> AnimationsDict, string key, string filename, int frames, Canvas levelCanvas) { Image[] newImages = new Image[frames]; for (int x = 0; x < frames; x++) { newImages[x] = SnakeUtils.LoadImage(filename + "_" + (x + 1)); newImages[x].Visibility = Visibility.Collapsed; levelCanvas.Children.Add(newImages[x]); } AnimationsDict.Add(key, newImages); }
public void _initCollisionAnimation() { _bufferedCollisionImage = new Image[CurLevel.Animation.CollisionFrames, 4]; for (int y = 0; y < 4; y++) { for (int x = 0; x < CurLevel.Animation.CollisionFrames; x++) { _bufferedCollisionImage[x, y] = SnakeUtils.LoadImage(CurLevel.Animation.FilenameCollision + "_" + (x + 1) + "_" + (y + 1)); _bufferedCollisionImage[x, y].Visibility = Visibility.Collapsed; CurLevel.LevelCanvas.Children.Add(_bufferedCollisionImage[x, y]); } } }
protected override void ReadAppleTag(Level creationLevel_DC, XmlReader reader, Apple apple_DC) { SLLevel creationLevel = creationLevel_DC as SLLevel; SLApple apple = apple_DC as SLApple; base.ReadAppleTag(creationLevel, reader, apple); reader.MoveToAttribute("img"); string appleFilename = reader.Value; reader.MoveToAttribute("offsetY"); apple.AppleOffsetY = Double.Parse(reader.Value); apple.AppleImage = SnakeUtils.LoadImage(appleFilename); creationLevel.LevelCanvas.Children.Add(apple.AppleImage); }
public void ShowWinner() { if (Animated) { _winAnimCount++; int frameNo = (_winAnimCount / CurLevel.Animation.WinDelay) % CurLevel.Animation.WinFrames; SnakeImage.Visibility = Visibility.Collapsed; SnakeImage = AnimationsDict["win"][frameNo]; SnakeImage.Visibility = Visibility.Visible; foreach (SLTail tail in TailList) { tail.TailImage.Visibility = Visibility.Collapsed; tail.TailImage = tail.AnimationsDict["win"][frameNo]; tail.TailImage.Visibility = Visibility.Visible; } if (_winAnimCount >= CurLevel.TileSize) { _winAnimCount = 0; } } else { if (_winAnimCount > 0) { return; } SnakeImage.Visibility = Visibility.Collapsed; SnakeImage = SnakeUtils.LoadImage(CurLevel.Animation.FilenameWin); CurLevel.LevelCanvas.Children.Add(SnakeImage); SnakeImage.Visibility = Visibility.Visible; foreach (SLTail tail in TailList) { tail.TailImage.Visibility = Visibility.Collapsed; tail.TailImage = SnakeUtils.LoadImage(CurLevel.Animation.FilenameWin); CurLevel.LevelCanvas.Children.Add(tail.TailImage); tail.TailImage.Visibility = Visibility.Visible; } _winAnimCount++; } }
private void _initAnimation() { _bufferedImage = new Image[CurLevel.Animation.AnimationFrames, 4]; for (int y = 0; y < 4; y++) { for (int x = 0; x < CurLevel.Animation.AnimationFrames; x++) { _bufferedImage[x, y] = SnakeUtils.LoadImage(CurLevel.Animation.FilenameTail + "_" + (x + 1) + "_" + (y + 1)); _bufferedImage[x, y].Visibility = Visibility.Collapsed; CurLevel.LevelCanvas.Children.Add(_bufferedImage[x, y]); } } TailImage = BufferedImage[0, 0]; TailImage.Visibility = Visibility.Visible; }
public void _initSnakeAnimation() { _bufferedImage = new Image[CurLevel.Animation.AnimationFrames, 4]; for (int y = 0; y < 4; y++) { for (int x = 0; x < CurLevel.Animation.AnimationFrames; x++) { _bufferedImage[x, y] = SnakeUtils.LoadImage(CurLevel.FilenameHead + "_" + (x + 1) + "_" + (y + 1)); _bufferedImage[x, y].Visibility = Visibility.Collapsed; CurLevel.LevelCanvas.Children.Add(_bufferedImage[x, y]); } } /* Start with first animation frame */ SnakeImage = BufferedImage[0, 0]; SnakeImage.Visibility = Visibility.Visible; }
protected override void ReadObjectTag(Level creationLevel_DC, XmlReader reader, LevelObject levObject_DC) { SLLevelObject levObject = levObject_DC as SLLevelObject; SLLevel creationLevel = creationLevel_DC as SLLevel; base.ReadObjectTag(creationLevel_DC, reader, levObject_DC); levObject.Pos = new Point((double)levObject.ObjectPosX * creationLevel.TileSize, (double)levObject.ObjectPosY * creationLevel.TileSize); reader.MoveToAttribute("offsetY"); levObject.OffsetY = Double.Parse(reader.Value); reader.MoveToAttribute("filename"); levObject.ObjectImage = SnakeUtils.LoadImage(reader.Value); levObject.ObjectImage.SetValue(Canvas.LeftProperty, levObject.Pos.X); levObject.ObjectImage.SetValue(Canvas.TopProperty, levObject.Pos.Y + levObject.OffsetY); creationLevel.LevelCanvas.Children.Add(levObject.ObjectImage); }
public SLTail(SnakeUtils.Directions direction, int tailPosX, int tailPosY, SLLevel curLevel) : base(direction, tailPosX, tailPosY, curLevel) { CurLevel = curLevel; _pos.X = (double)(tailPosX * CurLevel.TileSize); _pos.Y = (double)(tailPosY * CurLevel.TileSize); switch (direction) { case SnakeUtils.Directions.Up: _pos.Y += CurLevel.TileSize; break; case SnakeUtils.Directions.Right: _pos.X -= CurLevel.TileSize; break; case SnakeUtils.Directions.Down: _pos.Y -= CurLevel.TileSize; break; case SnakeUtils.Directions.Left: _pos.X += CurLevel.TileSize; break; } if (CurLevel.AnimatedSnake) { _initAnimation(); SnakeUtils.InitAnimationInDict(AnimationsDict, "win", CurLevel.Animation.FilenameWin, CurLevel.Animation.WinFrames, CurLevel.LevelCanvas); SnakeUtils.InitAnimationInDict(AnimationsDict, "grow", CurLevel.Animation.FilenameGrow, CurLevel.Animation.GrowFrames, CurLevel.LevelCanvas); SnakeUtils.InitAnimationInDict(AnimationsDict, "shrink", CurLevel.Animation.FilenameShrink, CurLevel.Animation.ShrinkFrames, CurLevel.LevelCanvas); } else { TailImage = SnakeUtils.LoadImage(curLevel.Animation.FilenameTail); CurLevel.LevelCanvas.Children.Add(TailImage); } TailImage.SetValue(Canvas.LeftProperty, Pos.X); TailImage.SetValue(Canvas.TopProperty, Pos.Y + CurLevel.SnakeOffsetY); }
public void MoveSL() { if (NewDirection == SnakeUtils.Directions.Stop) { return; } if (_isNewTurn == true) { base.ChangeDirection(); //if (!directionChanged && _keyBuffer.Count > 0) // _keyBuffer.Clear(); _UpdateKeyBuffer(); _isNewTurn = false; ImminentCollision = base.CollisionImminent(); } if (ImminentCollision != SnakeUtils.Collision.Wall && ImminentCollision != SnakeUtils.Collision.Object && ImminentCollision != SnakeUtils.Collision.Tail) { SnakeUtils.MoveSLPosition(OldDirection, ref _pos, 1.0); foreach (SLTail tail in TailList) { tail.SLMoveTail(TailList); } } _tileCount++; if (_tileCount >= CurLevel.TileSize) { base.Move(); SnakeUtils.SetZIndex(CurLevel, this, TailList); _isNewTurn = true; _tileCount = 0; } if (Animated) { int frameNo = (_tileCount / CurLevel.Animation.AnimationDelay) % CurLevel.Animation.AnimationFrames; int frameColl = (_tileCount / CurLevel.Animation.CollisionDelay) % CurLevel.Animation.CollisionFrames; SnakeImage.Visibility = Visibility.Collapsed; if (ImminentCollision != SnakeUtils.Collision.Wall && ImminentCollision != SnakeUtils.Collision.Object && ImminentCollision != SnakeUtils.Collision.Tail) { if (!CurLevel.IsControlInversed) { SnakeImage = BufferedImage[frameNo, ((int)this.OldDirection - 1)]; } else { switch (this.OldDirection) { case SnakeUtils.Directions.Up: SnakeImage = BufferedImage[frameNo, 2]; break; case SnakeUtils.Directions.Right: SnakeImage = BufferedImage[frameNo, 3]; break; case SnakeUtils.Directions.Down: SnakeImage = BufferedImage[frameNo, 0]; break; case SnakeUtils.Directions.Left: SnakeImage = BufferedImage[frameNo, 1]; break; } } foreach (SLTail tail in TailList) { tail.Animate(_tileCount); } } else { //Add here Collision Animation, e.g. when hitting an object SnakeImage = BufferedCollisionImage[frameColl, ((int)this.OldDirection - 1)]; } SnakeImage.Visibility = Visibility.Visible; } else { if (ImminentCollision == SnakeUtils.Collision.Wall || ImminentCollision == SnakeUtils.Collision.Object || ImminentCollision == SnakeUtils.Collision.Tail) { SnakeImage.Visibility = Visibility.Collapsed; SnakeImage = SnakeUtils.LoadImage("NutsTheme/collision_3_1"); CurLevel.LevelCanvas.Children.Add(SnakeImage); SnakeImage.Visibility = Visibility.Visible; } } }