private TileAnimationData containsAnimatedTile(int tile, SonicLevel sonLevel) { foreach (var an in sonLevel.TileAnimations) { var anin = an.AnimationTileIndex; var num = an.NumberOfTiles; if (tile >= anin && tile < anin + num) return an; } return null; }
/* obtainedRing = []; */ public Sonic() { Watcher = new Watcher(); physicsVariables = SonicConstants.Sonic(); var sonicManager = SonicManager.Instance; sonicLevel = sonicManager.SonicLevel; X = sonicLevel.StartPositions[0].X; Y = sonicLevel.StartPositions[0].Y; SensorManager = new SensorManager(); HaltSmoke = new List<Point>(); Rings = 7; SensorManager.CreateVerticalSensor("a", -9, 0, 36, "#F202F2"); SensorManager.CreateVerticalSensor("b", 9, 0, 36, "#02C2F2"); SensorManager.CreateVerticalSensor("c", -9, 0, -20, "#2D2C21"); SensorManager.CreateVerticalSensor("d", 9, 0, -20, "#C24222"); SensorManager.CreateHorizontalSensor("m1", 4, 0, -12, "#212C2E"); SensorManager.CreateHorizontalSensor("m2", 4, 0, 13, "#22Ffc1"); SpriteState = "normal"; myRec = new Rectangle(0, 0, 0, 0); sonicLastHitTick = int.MinValue; }
public void Load(SLData sonicLevel) { Loading = true; Status = "Decoding"; Status = "Determining Level Information"; SonicLevel = new SonicLevel(); for (var n = 0; n < sonicLevel.Rings.Length; n++) { SonicLevel.Rings[n] = new Ring(true); SonicLevel.Rings[n].X = sonicLevel.Rings[n].X; SonicLevel.Rings[n].Y = sonicLevel.Rings[n].Y; } SonicLevel.LevelWidth = sonicLevel.ForegroundWidth; SonicLevel.LevelHeight = sonicLevel.ForegroundHeight; SonicLevel.ChunkMap = sonicLevel.Foreground; SonicLevel.BGChunkMap = sonicLevel.Background; for (int l = 0; l < sonicLevel.Objects.Length; l++) { SonicLevel.Objects[l] = new LevelObjectInfo(sonicLevel.Objects[l]); SonicLevel.Objects[l].Index = l; } var objectKeys = new List<string>(); foreach (LevelObjectInfo t in SonicLevel.Objects) { var o = t.Key; if (objectKeys.All(p => p != o)) objectKeys.Add(o); } loadObjects(objectKeys); for (int j = 0; j < sonicLevel.Tiles.Length; j++) { var fc = sonicLevel.Tiles[j]; var tiles = fc; List<int> mj = new List<int>(); for (int i = 0; i < tiles.Length; i++) { var value = sonicLevel.Tiles[j][i]; mj.Add((value >> 4)); mj.Add((value & 0xF)); } var mfc = new int[8][]; for (int o = 0; o < 8; o++) { mfc[o] = new int[8]; } for (int n = 0; n < mj.Count; n++) { mfc[n % 8][n / 8] = mj[n]; } SonicLevel.Tiles[j] = new Tile(mfc); SonicLevel.Tiles[j].Index = j; } var acs = SonicLevel.AnimatedChunks = new List<TileChunk>(); if (sonicLevel.AnimatedFiles.Truthy()) { SonicLevel.AnimatedTileFiles = new Tile[sonicLevel.AnimatedFiles.Length][]; for (var animatedFileIndex = 0; animatedFileIndex < sonicLevel.AnimatedFiles.Length; animatedFileIndex++) { var animatedFile = sonicLevel.AnimatedFiles[animatedFileIndex]; SonicLevel.AnimatedTileFiles[animatedFileIndex] = new Tile[animatedFile.Length]; for (int filePiece = 0; filePiece < animatedFile.Length; filePiece++) { var c = animatedFile[filePiece]; var tiles = c; List<int> mjc = new List<int>(); for (int l = 0; l < tiles.Length; l++) { var value = animatedFile[filePiece][l]; mjc.Add((value >> 4)); mjc.Add((value & 0xF)); } var mfc = new int[8][]; for (int o = 0; o < 8; o++) { mfc[o] = new int[8]; } for (int n = 0; n < mjc.Count; n++) { mfc[n % 8][n / 8] = mjc[n]; } Tile tile = new Tile(mfc); tile.IsTileAnimated = true; tile.Index = filePiece * 10000 + animatedFileIndex; SonicLevel.AnimatedTileFiles[animatedFileIndex][filePiece] = tile; } } } for (int j = 0; j < sonicLevel.Blocks.Length; j++) { var fc = sonicLevel.Blocks[j]; var mj = new TilePiece(); mj.Index = j; mj.Tiles = new List<TileInfo>(); for (int p = 0; p < fc.Length; p++) { mj.Tiles.Add(new TileInfo() { _Tile = fc[p].Tile, Index = p, Palette = fc[p].Palette, Priority = fc[p].Priority, XFlip = fc[p].XFlip, YFlip = fc[p].YFlip, }); } mj.Init(); SonicLevel.TilePieces[j] = mj; } SonicLevel.Angles = sonicLevel.Angles; SonicLevel.TileAnimations = new List<TileAnimationData>( sonicLevel.Animations.Map( a => new TileAnimationData() { AnimationTileFile = a.AnimationFile, AnimationTileIndex = a.AnimationTileIndex, AutomatedTiming = a.AutomatedTiming, NumberOfTiles = a.NumberOfTiles, DataFrames = (TileAnimationDataFrame[]) a.Frames.Map( b => new TileAnimationDataFrame() { Ticks = b.Ticks, StartingTileIndex = b.StartingTileIndex }).Slice(0) })); SonicLevel.CollisionIndexes1 = sonicLevel.CollisionIndexes1; SonicLevel.CollisionIndexes2 = sonicLevel.CollisionIndexes2; for (int i = 0; i < sonicLevel.HeightMaps.Length; i++) { var b1 = true; var b2 = true; for (int m = 0; m < sonicLevel.HeightMaps[i].Length; m++) { if (b1 && sonicLevel.HeightMaps[i][m] != 0) b1 = false; if (b2 && sonicLevel.HeightMaps[i][m] != 16) b2 = false; } if (b1) SonicLevel.HeightMaps[i] = new HeightMap(false); else if (b2) SonicLevel.HeightMaps[i] = new HeightMap(true); else SonicLevel.HeightMaps[i] = new HeightMap(sonicLevel.HeightMaps[i], i); } for (int j = 0; j < sonicLevel.Chunks.Length; j++) { var fc = sonicLevel.Chunks[j]; var mj = new TileChunk(); mj.Index = j; mj.TilePieces = new TilePieceInfo[8][]; for (int i = 0; i < 8; i++) { mj.TilePieces[i] = new TilePieceInfo[8]; } for (int p = 0; p < fc.Length; p++) { mj.TilePieces[p % 8][p / 8] = new TilePieceInfo() { Index = p, Block = fc[p].Block, Solid1 = fc[p].Solid1, Solid2 = fc[p].Solid2, XFlip = fc[p].XFlip, YFlip = fc[p].YFlip }; } SonicLevel.TileChunks[j] = mj; mj.TileAnimations = new JsDictionary<int, TileAnimationData>(); for (int tpX = 0; tpX < mj.TilePieces.Length; tpX++) { for (int tpY = 0; tpY < mj.TilePieces[tpX].Length; tpY++) { var pm = mj.TilePieces[tpX][tpY].GetTilePiece(); if (pm != null) { foreach (var mjc in pm.Tiles) { var fa = containsAnimatedTile(mjc._Tile, SonicLevel); if (fa.Truthy()) { mj.TileAnimations[tpY * 8 + tpX] = fa; acs[j] = mj; } } } } } } SonicLevel.Palette = sonicLevel.Palette.Map(a => a.Map(b => "#"+b)); SonicLevel.StartPositions = sonicLevel.StartPositions.Map(a => new Point(a.X, a.Y)).Array(); SonicLevel.AnimatedPalettes = new List<PaletteItem>(); if (sonicLevel.PaletteItems.Length > 0) { for (int k = 0; k < sonicLevel.PaletteItems[0].Length; k++) { AnimatedPaletteItem pal = sonicLevel.PaletteItems[0][k]; SonicLevel.AnimatedPalettes.Add(new PaletteItem() { Palette = ((string[])Script.Eval(pal.Palette)).Map(b => "#" + b), SkipIndex = pal.SkipIndex, TotalLength = pal.TotalLength, Pieces = pal.Pieces.Map(a => new PaletteItemPieces() { PaletteIndex = a.PaletteIndex, PaletteMultiply = a.PaletteMultiply, PaletteOffset = a.PaletteOffset }) }); } } foreach (var tilePiece in SonicLevel.TilePieces) { tilePiece.AnimatedPaletteIndexes = new List<int>(); tilePiece.AnimatedTileIndexes = new List<int>(); if (SonicLevel.AnimatedPalettes.Count > 0) { foreach (var mj in tilePiece.Tiles) { Tile tile = mj.GetTile(); if (tile.Truthy()) { tile.AnimatedPaletteIndexes = new List<int>(); var pl = tile.GetAllPaletteIndexes(); tile.PaletteIndexesToBeAnimated = new JsDictionary<int, List<int>>(); tile.AnimatedTileIndexes = new List<int>(); for (int tileAnimationIndex = 0; tileAnimationIndex < SonicLevel.TileAnimations.Count; tileAnimationIndex++) { var tileAnimationData = SonicLevel.TileAnimations[tileAnimationIndex]; var anin = tileAnimationData.AnimationTileIndex; var num = tileAnimationData.NumberOfTiles; if (tile.Index >= anin && tile.Index < anin + num) { tilePiece.AnimatedTileIndexes.Add(tileAnimationIndex); tile.AnimatedTileIndexes.Add(tileAnimationIndex); } } for (int animatedPaletteIndex = 0; animatedPaletteIndex < SonicLevel.AnimatedPalettes.Count; animatedPaletteIndex++) { var pal = SonicLevel.AnimatedPalettes[animatedPaletteIndex]; tile.PaletteIndexesToBeAnimated[animatedPaletteIndex] = new List<int>(); foreach (var mjce in pal.Pieces) { PaletteItemPieces mje1 = mjce; if (mj.Palette == mje1.PaletteIndex) { if (pl.Any(j => j == mje1.PaletteOffset / 2 || j == mje1.PaletteOffset / 2 + 1)) { tilePiece.AnimatedPaletteIndexes.Add(animatedPaletteIndex); tile.AnimatedPaletteIndexes.Add(animatedPaletteIndex); foreach (var pIndex in pl) { if (pIndex == mje1.PaletteOffset / 2 || pIndex == mje1.PaletteOffset / 2 + 1) { tile.PaletteIndexesToBeAnimated[animatedPaletteIndex].Add(pIndex); } } } } } } } } } } var finished = new Action(() => { Loading = false; }); PreloadSprites(() => { finished(); ForceResize(); }, (s) => { }); ForceResize(); OnLevelLoad(SonicLevel); /* var finished = function () { sonicManager.uiManager.levelManagerArea.visible = true; sonicManager.loading = false; sonicManager.uiManager.modifyTC.tileChunk = sonicManager.SonicLevel.TileChunks[0]; sonicManager.uiManager.modifyTilePieceArea.tilePiece = sonicManager.uiManager.modifyTP.tilePiece = sonicManager.SonicLevel.TilePieces[0]; }; // var inds = sonicManager.inds = { r:0,t: 0, tp: 0, tc: 0, total: (sonicManager.SonicLevel.TileChunks.length * 2 + sonicManager.SonicLevel.TilePieces.length * 5 + sonicManager.SonicLevel.Tiles.length), done: false }; sonicManager.CACHING = true; sonicManager.preLoadSprites(scale, function () { // inds.r = 1; sonicManager.CACHING = false; finished(); sonicManager.uiManager.updateTitle("Level Loaded"); sonicManager.forceResize(); var dl = _H.getQueryString(); if (dl["run"]) { setTimeout(sonicManager.uiManager.runSonic, 1000); } }, sonicManager.uiManager.updateTitle); */ }
public void Tick(SonicLevel sonicLevel) { if (Debugging) { var debugSpeed = Watcher.Multiply(15); if (HoldingRight) X += debugSpeed; if (HoldingLeft) X -= debugSpeed; if (Crouching) Y += debugSpeed; if (HoldingUp) Y -= debugSpeed; X = ( ( sonicLevel.LevelWidth * 128 ) + ( X ) ) % ( sonicLevel.LevelWidth * 128 ) ; Y = ( ( sonicLevel.LevelHeight * 128 ) + ( Y ) ) % ( sonicLevel.LevelHeight * 128 ); return; } UpdateMode(); if (HLock > 0) { HLock--; HoldingRight = false; HoldingLeft = false; } if (InAir) { if (Angle != 0xff) { Angle = ( 0xff + ( Angle + ( ( Angle > 0xff / 2 ) ? 2 : -2 ) ) ) % 0xff; if (Angle >= 0xfd || Angle <= 0x01) Angle = 0xff; } } EffectPhysics(); CheckCollisionWithRings(); UpdateSprite(); SensorManager.Check(this); var sensorM1 = SensorManager.GetResult("m1"); var sensorM2 = SensorManager.GetResult("m2"); var best = GetBestSensor(sensorM1, sensorM2, Mode); if (best != null) { switch (Mode) { case RotationMode.Floor: X = ( best.Value + ( sensorM2 != null && sensorM1 != null && ( sensorM1.Value == sensorM2.Value ) ? 12 : ( best.Letter == "m1" ? 12 : -12 ) ) ); Gsp = 0; if (InAir) Xsp = 0; break; case RotationMode.LeftWall: Y = ( best.Value + ( sensorM2 != null && sensorM1 != null && ( sensorM1.Value == sensorM2.Value ) ? 12 : ( best.Letter == "m1" ? 12 : -12 ) ) ); if (InAir) Xsp = 0; break; case RotationMode.Ceiling: X = ( best.Value + ( sensorM2 != null && sensorM1 != null && ( sensorM1.Value == sensorM2.Value ) ? 12 : ( best.Letter == "m1" ? -12 : 12 ) ) ); Gsp = 0; if (InAir) Xsp = 0; break; case RotationMode.RightWall: Y = ( best.Value + ( sensorM2 != null && sensorM1 != null && ( sensorM1.Value == sensorM2.Value ) ? 12 : ( best.Letter == "m1" ? -12 : 12 ) ) ); Gsp = 0; if (InAir) Xsp = 0; break; } } SensorManager.Check(this); var sensorA = SensorManager.GetResult("a"); var sensorB = SensorManager.GetResult("b"); int fy; int fx; var hSize = GetHalfImageSize(); if (!InAir) { best = GetBestSensor(sensorA, sensorB, Mode); if (best == null) InAir = true; else { JustHit = false; switch (Mode) { case RotationMode.Floor: best.Chosen = true; Angle = best.Angle; Y = fy = best.Value - hSize.Y; break; case RotationMode.LeftWall: best.Chosen = true; Angle = best.Angle; X = fx = best.Value + hSize.X; break; case RotationMode.Ceiling: best.Chosen = true; Angle = best.Angle; Y = fy = best.Value + hSize.Y; break; case RotationMode.RightWall: best.Chosen = true; Angle = best.Angle; X = fx = best.Value - hSize.X; break; } } UpdateMode(); } else { if (sensorA == null && sensorB == null) InAir = true; else { if (( sensorA != null && sensorA.Value >= 0 ) && ( sensorB != null && sensorB.Value >= 0 )) { if (sensorA.Value < sensorB.Value) { if (Y + ( 20 ) >= sensorA.Value) { Angle = sensorA.Angle; Y = fy = sensorA.Value - hSize.Y; Rolling = CurrentlyBall = false; InAir = false; } } else { if (sensorB.Value > -1) { if (Y + ( 20 ) >= sensorB.Value) { Angle = sensorB.Angle; Y = fy = sensorB.Value - hSize.Y; Rolling = CurrentlyBall = false; InAir = false; } } } } else if (( sensorA != null ) && sensorA.Value > -1) { if (Y + ( 20 ) >= sensorA.Value) { Angle = sensorA.Angle; Y = fy = sensorA.Value - hSize.Y; Rolling = CurrentlyBall = false; InAir = false; } } else if (sensorB != null && sensorB.Value > -1) { if (Y + ( 20 ) >= sensorB.Value) { Angle = sensorB.Angle; Y = fy = sensorB.Value - hSize.Y; Rolling = CurrentlyBall = false; InAir = false; } } } UpdateMode(); var cur = SonicManager.Instance.SpriteCache.SonicSprites[SpriteState]; var __h = cur.Height / 2; SensorManager.Check(this); var sensorC = SensorManager.GetResult("c"); var sensorD = SensorManager.GetResult("d"); if (( sensorC == null && sensorD == null )) {} else { if (sensorD != null && ( sensorC != null ) && ( sensorC.Value >= 0 && sensorD.Value >= 0 )) { if (sensorC.Value < sensorD.Value) { if (Y + ( __h ) >= sensorC.Value) { if (Ysp < 0) { if (sensorC.Angle > 0x40 && sensorC.Angle < 0xC0) { Angle = sensorC.Angle; Gsp = Ysp; InAir = false; WasInAir = false; } else Ysp = 0; Y = fy = sensorC.Value + __h; } } } else { if (Y + ( __h ) >= sensorD.Value) { if (Ysp < 0) { if (sensorD.Angle > 0x40 && sensorD.Angle < 0xC0) { Angle = sensorD.Angle; Gsp = -Ysp; InAir = false; WasInAir = false; } else Ysp = 0; Y = fy = sensorD.Value + __h; } } } } else if (sensorC != null && sensorC.Value > -1) { if (Y + ( __h ) >= sensorC.Value) { if (Ysp < 0) { if (sensorC.Angle > 0x40 && sensorC.Angle < 0xC0) { Angle = sensorC.Angle; Gsp = Ysp; InAir = false; WasInAir = false; } else Ysp = 0; Y = fy = sensorC.Value + __h; } } } else if (sensorD != null && sensorD.Value > -1) { if (Y + ( __h ) >= sensorD.Value) { if (Ysp < 0) { if (sensorD.Angle > 0x40 && sensorD.Angle < 0xC0) { Angle = sensorD.Angle; Gsp = -Ysp; InAir = false; WasInAir = false; } else Ysp = 0; Y = fy = sensorD.Value + __h; } } } UpdateMode(); } } }