using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Microsoft.Xna.Framework;
using Revitalize.Framework.Objects.InformationFiles;
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
{
///
/// A static reference to the resource manager for quicker access.
///
public static ResourceManager self;
///
/// A list of all of the ores held by the resource manager.
///
public Dictionary ores;
public List visitedFloors;
///
/// Constructor.
///
public ResourceManager()
{
self = this;
this.ores = new Dictionary();
this.visitedFloors = new List();
this.loadOreVeins();
}
///
/// Loads in all of the ore veins for the game.
///
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));
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()
{
new IntRange(1,9)
},new List(),1,5,1,10,1d,1d,0,0,0,0),new List()));
this.ores.Add("Omegasis.Revitalize.Resources.Ore.Test", testOre);
}
///
/// Spawns an ore vein at the given location if possible.
///
///
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)
{
//ModCore.log("Location is: " + Location.Name);
spawn.placementAction(Location, (int)TilePosition.X*Game1.tileSize, (int)TilePosition.Y*Game1.tileSize,Game1.player);
}
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);
}
}
///
/// Spawns an orevein at the tile position at the same location as the player.
///
///
///
///
public bool spawnOreVein(string name, Vector2 TilePosition)
{
return this.spawnOreVein(name, Game1.player.currentLocation, TilePosition);
}
///
/// Checks to see if a resource can be spawned here.
///
///
///
///
///
public bool canResourceBeSpawnedHere(MultiTiledObject OBJ,GameLocation Location, Vector2 TilePosition)
{
return OBJ.canBePlacedHere(Location, TilePosition) && Location.isTileLocationTotallyClearAndPlaceable(TilePosition);
}
//~~~~~~~~~~~~~~~~~~~~~~~//
// Mine ore spawn code //
//~~~~~~~~~~~~~~~~~~~~~~~//
#region
public void spawnOreInMine()
{
int floorLevel = LocationUtilities.CurrentMineLevel();
if (this.hasVisitedFloor(floorLevel))
{
//Already has spawned ores for this visit.
return;
}
else
{
this.visitedFloors.Add(floorLevel);
}
List spawnableOreVeins = new List();
//Get a list of all of the ores that can spawn on this mine level.
foreach(KeyValuePair pair in this.ores)
{
if (pair.Value.resourceInfo.canSpawnAtLocation() && (pair.Value.resourceInfo as OreResourceInformation).canSpawnOnCurrentMineLevel())
{
spawnableOreVeins.Add(pair.Value);
}
}
foreach(OreVeinObj ore in spawnableOreVeins)
{
if (ore.resourceInfo.shouldSpawn())
{
int amount = ore.resourceInfo.getNumberOfNodesToSpawn();
List 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.
}
}
}
else
{
//Ore doesn't meet spawn chance.
}
//ModCore.log("Spawned :" + amount + " pancake test ores!");
}
}
///
/// Checks to see if the player has visited the given floor.
///
///
///
public bool hasVisitedFloor(int Floor)
{
return this.visitedFloors.Contains(Floor);
}
///
/// What happens when the player warps maps.
///
///
///
public void OnPlayerLocationChanged(object o,EventArgs playerWarped)
{
this.spawnOreInMine();
if(LocationUtilities.IsPlayerInMine()==false && LocationUtilities.IsPlayerInSkullCave() == false && LocationUtilities.IsPlayerInMineEnterance()==false)
{
this.visitedFloors.Clear();
}
}
#endregion
}
}