protected virtual void InitializePixelTileObstacleList() { if (PixelTileObstacleList == null) { PixelTileObstacleList = new List <PixelTile>(); } PixelTile[] allPixelTiles = FindObjectsOfType <PixelTile>(); int pixelTileCount = allPixelTiles.Length; if (pixelTileCount == CurrentPixelTileCount) { return; } PixelTileObstacleList.Clear(); for (int i = 0; i < pixelTileCount; ++i) { PixelTile pixelTileComponent = allPixelTiles[i]; if ((pixelTileComponent.LevelFlags & LevelFlags.OBSTACLE) == LevelFlags.OBSTACLE) { PixelTileObstacleList.Add(pixelTileComponent); } } CurrentPixelTileCount = pixelTileCount; }
private static void CreatePixelCharacterInstance() { GameObject pixelTileGameObject = new GameObject("Pixel Character"); PixelTile pixelTileComponent = pixelTileGameObject.AddComponent <PixelCharacter>().GetComponent <PixelTile>(); PixelLevel firstPixelLevelFound = GameObject.FindObjectOfType <PixelLevel>(); if (firstPixelLevelFound) { pixelTileComponent.CurrentPixelLevelInstance = firstPixelLevelFound; } }
private void DetectAndResolveCollisions() { int pixelsPerUnit = CurrentPixelLevelInstance.PixelsPerUnit; int tileSizeX = PixelTileComponent.TileSizeX; int tileSizeY = PixelTileComponent.TileSizeY; for (int j = 0; j < PixelTileObstacleList.Count; ++j) { PixelTile obstaclePixelTileComponent = PixelTileObstacleList[j]; int obstacleMinimumX = CurrentPixelLevelInstance.PixelOriginX + obstaclePixelTileComponent.AlignedRelativePositionX * pixelsPerUnit; int obstacleMaximumX = obstacleMinimumX + obstaclePixelTileComponent.TileSizeX * pixelsPerUnit; int obstacleMinimumY = CurrentPixelLevelInstance.PixelOriginY + obstaclePixelTileComponent.AlignedRelativePositionY * pixelsPerUnit; int obstacleMaximumY = obstacleMinimumY + obstaclePixelTileComponent.TileSizeY * pixelsPerUnit; int pixelMinimumX = PixelPositionX; int pixelMaximumX = PixelPositionX + tileSizeX * pixelsPerUnit; int pixelMinimumY = PixelPositionY; int pixelMaximumY = PixelPositionY + tileSizeY * pixelsPerUnit; if ( obstacleMinimumY > pixelMaximumY || pixelMinimumY > obstacleMaximumY || obstacleMinimumX > pixelMaximumX || pixelMinimumX > obstacleMaximumX ) { OnObstacleExited(obstaclePixelTileComponent.GetInstanceID()); continue; } int firstPixelDifferenceX = pixelMaximumX - obstacleMinimumX; int secondPixelDifferenceX = obstacleMaximumX - pixelMinimumX; int firstPixelDifferenceY = pixelMaximumY - obstacleMinimumY; int secondPixelDifferenceY = obstacleMaximumY - pixelMinimumY; int[] differences = new int[4] { firstPixelDifferenceX, secondPixelDifferenceX, firstPixelDifferenceY, secondPixelDifferenceY }; int differenceMinimum = differences.Min(); if (differenceMinimum == differences[0]) { OnObstacleCollided(obstaclePixelTileComponent.GetInstanceID(), CollisionNormalFlags.RIGHT_WALL); PixelPositionX -= differences[0]; } if (differenceMinimum == differences[1]) { OnObstacleCollided(obstaclePixelTileComponent.GetInstanceID(), CollisionNormalFlags.LEFT_WALL); PixelPositionX += differences[1]; } if (differenceMinimum == differences[2]) { OnObstacleCollided(obstaclePixelTileComponent.GetInstanceID(), CollisionNormalFlags.CEILING); PixelPositionY -= differences[2]; } if (differenceMinimum == differences[3]) { isJumping = false; OnObstacleCollided(obstaclePixelTileComponent.GetInstanceID(), CollisionNormalFlags.GROUND); PixelPositionY += differences[3]; } } }