public PlayerData get(IMyPlayer player) { PlayerData result; if(!mPlayerData.TryGetValue(player.SteamUserId , out result)) { result = new PlayerData(player.SteamUserId); mPlayerData.Add(player.SteamUserId , result); } return result; }
public void Save() { try { PlayerData[] tmp = new PlayerData[mPlayerData.Count]; mPlayerData.Values.CopyTo(tmp, 0); TextWriter writer = MyAPIGateway.Utilities.WriteFileInWorldStorage(mFilename, typeof(PlayerDataStore)); writer.Write(MyAPIGateway.Utilities.SerializeToXML<PlayerData[]>(tmp)); writer.Flush(); writer.Close(); } catch (Exception e) { //MyAPIGateway.Utilities.ShowMessage("ERROR", "Error: " + e.Message + "\n" + e.StackTrace); } }
private void FoodUpdateMsgHandler(byte[] data) { //MyAPIGateway.Utilities.ShowMessage("Debug", "Heartbeat: " + mHud.Heartbeat); mPlayerData = MyAPIGateway.Utilities.SerializeFromXML<PlayerData>(Encoding.Unicode.GetString(data)); //MyAPIGateway.Utilities.ShowMessage("FoodSystem", "Hunger: " + Math.Floor(mPlayerData.hunger) + "% Thirst: " + Math.Floor(mPlayerData.thirst) + "%"); if (mPlayerData.thirst <= 10 && mPlayerData.hunger <= 10) { ShowNotification("Warning: You are Thirsty (" + Math.Floor(mPlayerData.thirst) + "%) and Hungry (" + Math.Floor(mPlayerData.hunger) + "%)", MyFontEnum.Red); } else if(mPlayerData.thirst <= 10) { ShowNotification("Warning: You are Thirsty (" + Math.Floor(mPlayerData.thirst) + "%)", MyFontEnum.Red); } else if(mPlayerData.hunger <= 10) { ShowNotification("Warning: You are Hungry (" + Math.Floor(mPlayerData.hunger) + "%)", MyFontEnum.Red); } if (mHud.Heartbeat) { mHud.CreateAndSend(1, (mPlayerData.thirst <= 10) ? 10 : 1000, new Vector2D(-0.98f, -0.15f), "Thirst: " + ((mPlayerData.thirst <= 10) ? "<color=255,0,0>" : "<color=0,255,0>") + Math.Floor(mPlayerData.thirst) + "%"); mHud.CreateAndSend(2, (mPlayerData.hunger <= 10) ? 10 : 1000, new Vector2D(-0.98f, -0.2f), "Hunger: " + ((mPlayerData.hunger <= 10) ? " <color=255,0,0>" : "<color=0,255,0>") + Math.Floor(mPlayerData.hunger) + "% "); } }
private static bool playerEatSomething(IMyEntity entity, PlayerData playerData) { MyInventoryBase inventory = ((MyEntity)entity).GetInventoryBase(); var items = inventory.GetItems(); foreach (IMyInventoryItem item in items) { float result; // Getting the item type string szItemContent = item.Content.ToString(); string szTypeName = szItemContent.Substring(szItemContent.IndexOf(OBJECT_BUILDER_PREFIX) + OBJECT_BUILDER_PREFIX.Length); // Type verification if (!szTypeName.Equals("Ingot")) continue; if (mFoodTypes.TryGetValue(item.Content.SubtypeName, out result)) { float canConsumeNum = Math.Min(((MAX_VALUE - playerData.hunger) / result), (float)item.Amount); //MyAPIGateway.Utilities.ShowMessage("DEBUG", "canEat: " + canConsumeNum); if (canConsumeNum > 0) { inventory.Remove(item, (MyFixedPoint)canConsumeNum); playerData.hunger += result * (float)canConsumeNum; return true; } } } return false; }