2019-08-14 10:05:11 +08:00
using System ;
using System.Collections.Generic ;
using System.Linq ;
using System.Text ;
using System.Threading.Tasks ;
using Microsoft.Xna.Framework ;
2019-08-14 14:06:51 +08:00
using Revitalize.Framework.Objects.InformationFiles ;
2019-08-14 10:05:11 +08:00
using Revitalize.Framework.Objects.Resources.OreVeins ;
using Revitalize.Framework.Utilities ;
using StardewValley ;
using StardustCore.Animations ;
using StardustCore.UIUtilities ;
namespace Revitalize.Framework.Objects
{
public class ResourceManager
{
/// <summary>
/// A static reference to the resource manager for quicker access.
/// </summary>
public static ResourceManager self ;
/// <summary>
/// A list of all of the ores held by the resource manager.
/// </summary>
public Dictionary < string , OreVeinObj > ores ;
2019-08-15 11:21:44 +08:00
public List < int > visitedFloors ;
2019-08-14 10:05:11 +08:00
/// <summary>
/// Constructor.
/// </summary>
public ResourceManager ( )
{
self = this ;
this . ores = new Dictionary < string , OreVeinObj > ( ) ;
2019-08-15 11:21:44 +08:00
this . visitedFloors = new List < int > ( ) ;
2019-08-14 10:05:11 +08:00
this . loadOreVeins ( ) ;
}
/// <summary>
/// Loads in all of the ore veins for the game.
/// </summary>
private void loadOreVeins ( )
{
//The pancake ore.
OreVeinObj testOre = new OreVeinObj ( PyTKHelper . CreateOBJData ( "Omegasis.Revitalize.Resources.Ore.Test" , TextureManager . GetTexture ( ModCore . Manifest , "Resources.Ore" , "Test" ) , typeof ( OreVeinTile ) , Color . White ) , new BasicItemInformation ( "Test Ore Vein" , "Omegasis.Revitalize.Resources.Ore.Test" , "A ore vein that is used for testing purposes." , "Revitalize.Ore" , Color . Black , - 300 , 0 , false , 350 , Vector2 . Zero , true , true , TextureManager . GetTexture ( ModCore . Manifest , "Resources.Ore" , "Test" ) , new AnimationManager ( TextureManager . GetExtendedTexture ( ModCore . Manifest , "Resources.Ore" , "Test" ) , new Animation ( 0 , 0 , 16 , 16 ) ) , Color . White , false , null , null ) ) ;
2019-08-15 05:32:22 +08:00
testOre . addComponent ( new Vector2 ( 0 , 0 ) , new OreVeinTile ( PyTKHelper . CreateOBJData ( "Omegasis.Revitalize.Resources.Ore.Test" , TextureManager . GetTexture ( ModCore . Manifest , "Resources.Ore" , "Test" ) , typeof ( OreVeinTile ) , Color . White ) , new BasicItemInformation ( "Test Ore Vein" , "Omegasis.Revitalize.Resources.Ore.Test" , "A ore vein that is used for testing purposes." , "Revitalize.Ore" , Color . Black , - 300 , 0 , false , 350 , Vector2 . Zero , true , true , TextureManager . GetTexture ( ModCore . Manifest , "Resources.Ore" , "Test" ) , new AnimationManager ( TextureManager . GetExtendedTexture ( ModCore . Manifest , "Resources.Ore" , "Test" ) , new Animation ( 0 , 0 , 16 , 16 ) ) , Color . White , false , null , null ) , new InformationFiles . OreResourceInformation ( new StardewValley . Object ( 211 , 1 ) , false , false , true , false , new List < IntRange > ( )
2019-08-14 14:06:51 +08:00
{
new IntRange ( 1 , 9 )
2019-08-15 05:32:22 +08:00
} , new List < IntRange > ( ) , 1 , 5 , 1 , 10 , 1d , 1d , 0 , 0 , 0 , 0 ) , new List < ResourceInformaton > ( ) ) ) ;
2019-08-14 14:06:51 +08:00
this . ores . Add ( "Omegasis.Revitalize.Resources.Ore.Test" , testOre ) ;
2019-08-14 10:05:11 +08:00
}
/// <summary>
/// Spawns an ore vein at the given location if possible.
/// </summary>
/// <param name="name"></param>
public bool spawnOreVein ( string name , GameLocation Location , Vector2 TilePosition )
{
if ( this . ores . ContainsKey ( name ) )
{
OreVeinObj spawn ;
this . ores . TryGetValue ( name , out spawn ) ;
if ( spawn ! = null )
{
spawn = ( OreVeinObj ) spawn . getOne ( ) ;
bool spawnable = this . canResourceBeSpawnedHere ( spawn , Location , TilePosition ) ;
if ( spawnable )
{
2019-08-14 10:26:20 +08:00
//ModCore.log("Location is: " + Location.Name);
spawn . placementAction ( Location , ( int ) TilePosition . X * Game1 . tileSize , ( int ) TilePosition . Y * Game1 . tileSize , Game1 . player ) ;
2019-08-14 10:05:11 +08:00
}
else
{
ModCore . log ( "Can't spawn ore: " + name + "at tile location: " + TilePosition ) ;
}
return spawnable ;
}
ModCore . log ( "Key doesn't exist. Weird." ) ;
return false ;
}
else
{
throw new Exception ( "The ore dictionary doesn't contain they key for resource: " + name ) ;
}
}
/// <summary>
/// Spawns an orevein at the tile position at the same location as the player.
/// </summary>
/// <param name="name"></param>
/// <param name="TilePosition"></param>
/// <returns></returns>
public bool spawnOreVein ( string name , Vector2 TilePosition )
{
return this . spawnOreVein ( name , Game1 . player . currentLocation , TilePosition ) ;
}
/// <summary>
/// Checks to see if a resource can be spawned here.
/// </summary>
/// <param name="OBJ"></param>
/// <param name="Location"></param>
/// <param name="TilePosition"></param>
/// <returns></returns>
2019-08-14 10:26:20 +08:00
public bool canResourceBeSpawnedHere ( MultiTiledObject OBJ , GameLocation Location , Vector2 TilePosition )
2019-08-14 10:05:11 +08:00
{
2019-08-14 14:06:51 +08:00
return OBJ . canBePlacedHere ( Location , TilePosition ) & & Location . isTileLocationTotallyClearAndPlaceable ( TilePosition ) ;
2019-08-14 10:05:11 +08:00
}
2019-08-14 11:13:27 +08:00
//~~~~~~~~~~~~~~~~~~~~~~~//
// Mine ore spawn code //
//~~~~~~~~~~~~~~~~~~~~~~~//
#region
public void spawnOreInMine ( )
{
int floorLevel = LocationUtilities . CurrentMineLevel ( ) ;
2019-08-15 11:21:44 +08:00
if ( this . hasVisitedFloor ( floorLevel ) )
{
//Already has spawned ores for this visit.
return ;
}
else
{
this . visitedFloors . Add ( floorLevel ) ;
}
2019-08-14 14:06:51 +08:00
List < OreVeinObj > spawnableOreVeins = new List < OreVeinObj > ( ) ;
//Get a list of all of the ores that can spawn on this mine level.
foreach ( KeyValuePair < string , OreVeinObj > pair in this . ores )
{
if ( pair . Value . resourceInfo . canSpawnAtLocation ( ) & & ( pair . Value . resourceInfo as OreResourceInformation ) . canSpawnOnCurrentMineLevel ( ) )
{
spawnableOreVeins . Add ( pair . Value ) ;
}
}
foreach ( OreVeinObj ore in spawnableOreVeins )
{
2019-08-15 11:21:44 +08:00
if ( ore . resourceInfo . shouldSpawn ( ) )
2019-08-14 14:06:51 +08:00
{
2019-08-15 11:21:44 +08:00
int amount = ore . resourceInfo . getNumberOfNodesToSpawn ( ) ;
List < Vector2 > openTiles = LocationUtilities . GetOpenObjectTiles ( Game1 . player . currentLocation , ( OreVeinObj ) ore . getOne ( ) ) ;
amount = Math . Min ( amount , openTiles . Count ) ; //Only spawn for as many open tiles or the amount of nodes to spawn.
for ( int i = 0 ; i < amount ; i + + )
{
int position = Game1 . random . Next ( openTiles . Count ) ;
bool didSpawn = this . spawnOreVein ( ore . info . id , openTiles [ position ] ) ;
if ( didSpawn = = false )
{
i - - ; //If the tile didn't spawn due to some odd reason ensure that the amount is spawned.
}
else
{
openTiles . Remove ( openTiles [ position ] ) ; //Remove that tile from the list of open tiles.
}
}
2019-08-14 14:06:51 +08:00
}
2019-08-15 11:21:44 +08:00
else
2019-08-14 11:13:27 +08:00
{
2019-08-15 11:21:44 +08:00
//Ore doesn't meet spawn chance.
2019-08-14 11:13:27 +08:00
}
2019-08-15 11:21:44 +08:00
//ModCore.log("Spawned :" + amount + " pancake test ores!");
2019-08-14 11:13:27 +08:00
}
2019-08-15 11:21:44 +08:00
2019-08-14 11:13:27 +08:00
}
2019-08-15 11:21:44 +08:00
/// <summary>
/// Checks to see if the player has visited the given floor.
/// </summary>
/// <param name="Floor"></param>
/// <returns></returns>
public bool hasVisitedFloor ( int Floor )
{
return this . visitedFloors . Contains ( Floor ) ;
}
/// <summary>
/// What happens when the player warps maps.
/// </summary>
/// <param name="o"></param>
/// <param name="playerWarped"></param>
2019-08-14 11:13:27 +08:00
public void OnPlayerLocationChanged ( object o , EventArgs playerWarped )
{
this . spawnOreInMine ( ) ;
2019-08-15 11:21:44 +08:00
if ( LocationUtilities . IsPlayerInMine ( ) = = false & & LocationUtilities . IsPlayerInSkullCave ( ) = = false & & LocationUtilities . IsPlayerInMineEnterance ( ) = = false )
{
this . visitedFloors . Clear ( ) ;
}
2019-08-14 11:13:27 +08:00
}
#endregion
2019-08-14 10:05:11 +08:00
}
}