using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;
using VBXSE;
using System.IO;
namespace ConqueringOneself
{
///
/// This is the main type for your game
///
public class Main : Microsoft.Xna.Framework.Game
{
public Main()
{
Content.RootDirectory = "Content";
SpriteEngine.Initialize(this, 800, 600);
}
protected override void Initialize()
{
Input.Initialize(this);
Combat.Initialize(this);
base.Initialize();
}
protected override void LoadContent()
{
SpriteEngine.LoadContent();
Audio.LoadContent(this);
FirstStart();
}
protected override void UnloadContent()
{
}
protected override void Update(GameTime gameTime)
{
Input.Update(gameTime);
Graphics.Update(gameTime);
SpriteEngine.Update(gameTime);
base.Update(gameTime);
}
protected override void Draw(GameTime gameTime)
{
GraphicsDevice.Clear(Color.Black);
SpriteEngine.Draw(gameTime);
base.Draw(gameTime);
}
//------------------------------
public enum GameState
{
None,
SelectingMove,
HandlingText,
ClickToContinue,
TitleScreen,
LevelUp,
Tutorial,
NameEntry
}
public enum BattleState
{
Normal,
Victorious,
Defeat,
OutOfBattle
}
public const int LEVELUP_MOVE_CHOICES = 3;
public GText nameEntry1 = null;
public GText nameEntry2 = null;
public Character player;
public Character enemy;
public List enemyPattern;
public int enemyPtnCounter = 0;
public List yourAttacks;
public int level = 1;
public int victories = 0;
public int losses = 0;
public int tutorialState = 0;
public Sprite tutorialSprite;
public int userID = -1;
public int selectedItem = 0;
public GameState gameState = GameState.None;
public BattleState battleState = BattleState.Normal;
public List levelUpAttacks = new List();
public void FirstStart()
{
Graphics.Initialize(this);
InitializeCharacter();
InitializeEnemy();
InitializeEnemyMoveListFirst();
LoadData();
if (tutorialState > 3) GoToTitleScreen();
else
{
HideAll();
if (player.name == "") NameEntry();
else Newbie();
}
}
public void HideAll()
{
Graphics.HideBackground();
Graphics.HideCombatStuff();
Graphics.HideDemonLord();
Graphics.HideLevelUp();
Graphics.HideMoves();
Graphics.HideTitle();
}
public void NameEntry()
{
gameState = GameState.NameEntry;
nameEntry1 = SpriteEngine.CreateGText("Hero, enter your name:", 250, 270);
nameEntry2 = SpriteEngine.CreateGText("_", 300, 300);
}
public void Newbie()
{
tutorialState = 0;
gameState = GameState.Tutorial;
Audio.PlayMusic("RuleOfTheDemonLord");
tutorialSprite = SpriteEngine.CreateSprite("intro", Vector2.Zero);
tutorialSprite.depth = -2000;
}
public void InitializeCharacter()
{
List starterAttacks = new List();
starterAttacks.Add(new Attack(0)); //Attack
starterAttacks.Add(new Attack(1)); //Defend
starterAttacks.Add(new Attack(2)); //Counterstrike
string alreadyName = "";
if (player != null)
{
alreadyName = player.name;
}
player = new Character(alreadyName, 100, 100, starterAttacks);
}
public void InitializeEnemy()
{
enemy = new Character("Demon Lord", (int)Math.Round((float)player.maxHP * 1.5f), (int)Math.Round((float)player.maxMP * 1.5f), player.availableAttacks);
enemy.id = 1;
enemyPtnCounter = 0;
yourAttacks = new List();
}
public void InitializeEnemyMoveListFirst()
{
enemyPattern = new List();
enemyPattern.Add(new Attack(1)); //Defend
enemyPattern.Add(new Attack(0)); //Attack
}
public void MoveSelection(int direction)
{
Audio.PlaySound("blip");
selectedItem += direction;
if (gameState == GameState.SelectingMove)
{
while (selectedItem < 0) selectedItem += player.availableAttacks.Count;
while (selectedItem >= player.availableAttacks.Count) selectedItem -= player.availableAttacks.Count;
Graphics.DisplayMoves();
}
else if (gameState == GameState.TitleScreen)
{
Graphics.scroll = 0;
while (selectedItem < 0) selectedItem += Graphics.title_options.Count;
while (selectedItem >= Graphics.title_options.Count) selectedItem -= Graphics.title_options.Count;
Graphics.DisplayTitle();
}
else if (gameState == GameState.LevelUp)
{
while (selectedItem < 0) selectedItem += LEVELUP_MOVE_CHOICES;
while (selectedItem >= LEVELUP_MOVE_CHOICES) selectedItem -= LEVELUP_MOVE_CHOICES;
Graphics.DisplayLevelUp();
}
}
public void Select()
{
Audio.PlaySound("select");
if (gameState == GameState.SelectingMove)
{
yourAttacks.Add(player.availableAttacks[selectedItem]);
Graphics.HideMoves();
gameState = GameState.HandlingText;
Combat.HandleTurn();
}
else if (gameState == GameState.TitleScreen)
{
switch (Graphics.TITLE_MENU_OPTIONS[selectedItem])
{
case "FIGHT THE DEMON LORD":
StartCombat();
break;
case "EXIT":
this.Exit();
break;
}
}
else if (gameState == GameState.LevelUp)
{
player.availableAttacks.Add(levelUpAttacks[selectedItem]);
level++;
if (tutorialState > 3)
{
GoToTitleScreen();
}
else
{
Audio.PlayMusic("RuleOfTheDemonLord");
tutorialSprite = SpriteEngine.CreateSprite("postbattle", Vector2.Zero);
tutorialSprite.depth = -2000;
gameState = GameState.Tutorial;
tutorialState++;
}
}
}
public void StartCombat()
{
Graphics.HideTitle();
InitializeEnemy();
Combat.StartCombat();
Graphics.DisplayMoves();
gameState = GameState.SelectingMove;
}
public void FinishText()
{
switch (battleState)
{
case BattleState.Normal:
Graphics.DisplayMoves();
gameState = GameState.SelectingMove;
break;
case BattleState.Victorious:
enemyPattern = yourAttacks;
victories++;
LevelUp();
break;
case BattleState.Defeat:
losses++;
if (tutorialState > 3)
GoToTitleScreen();
else
{
tutorialState = -1;
gameState = GameState.Tutorial;
tutorialSprite = SpriteEngine.CreateSprite("failure", Vector2.Zero);
tutorialSprite.depth = -2000;
}
break;
}
}
public void GoToTitleScreen()
{
selectedItem = 0;
Graphics.EraseCombatLog();
Graphics.HideBackground();
Graphics.HideDemonLord();
Graphics.HideMoves();
Graphics.HideCombatStuff();
Graphics.HideLevelUp();
if (userID == -1) userID = HighScores.GetID();
if (userID != -1) HighScores.SubmitScore(userID, player.name, level);
HighScores.UpdateScores();
Graphics.DisplayTitle();
Audio.PlayMusic("RuleOfTheDemonLord");
gameState = GameState.TitleScreen;
battleState = BattleState.OutOfBattle;
SaveData();
}
public void LevelUp()
{
selectedItem = 0;
Audio.PlayMusic("LevelUp");
int HP_increase = (int)Math.Round((float)player.maxHP * 0.25f);
int MP_increase = (int)Math.Round((float)player.maxMP * 0.05f);
player.maxHP += HP_increase;
player.maxMP += MP_increase;
Attack.InitializeUnlearned(player.availableAttacks);
levelUpAttacks = Attack.GetMovesToLearn();
Graphics.SetLevelUpAttacks(levelUpAttacks);
Graphics.DisplayLevelUp(HP_increase, MP_increase);
gameState = GameState.LevelUp;
battleState = BattleState.OutOfBattle;
}
public void LoadData()
{
if (File.Exists("save.dat"))
{
StreamReader sr = new StreamReader("save.dat");
string mode = "";
while (!mode.Contains("[end]"))
{
string line = sr.ReadLine();
if (line.StartsWith("["))
{
mode = line;
if (mode == "[attack]") player.availableAttacks.Clear();
if (mode == "[pattern]") enemyPattern.Clear();
}
else if (line != "")
{
switch (mode)
{
case "[stat]":
string[] split = line.Split(new string[] { "=" }, StringSplitOptions.None);
if (split.Length == 2)
{
switch (split[0])
{
case "hp": player.maxHP = Convert.ToInt32(split[1]); break;
case "mp": player.maxMP = Convert.ToInt32(split[1]); break;
case "level": level = Convert.ToInt32(split[1]); break;
case "id": userID = Convert.ToInt32(split[1]); break;
case "victories": victories = Convert.ToInt32(split[1]); break;
case "defeats": losses = Convert.ToInt32(split[1]); break;
case "tutorial": tutorialState = Convert.ToInt32(split[1]); break;
case "name": player.name = split[1]; break;
}
}
break;
case "[attack]":
player.availableAttacks.Add(new Attack(Convert.ToInt32(line)));
break;
case "[pattern]":
enemyPattern.Add(new Attack(Convert.ToInt32(line)));
break;
}
}
}
sr.Close();
}
}
public void SaveData()
{
FileStream fs = new FileStream("save.dat", FileMode.Create);
StreamWriter sw = new StreamWriter(fs);
//Write player's stat
sw.WriteLine("[stat]");
sw.WriteLine("name=" + player.name);
sw.WriteLine("hp=" + player.maxHP);
sw.WriteLine("mp=" + player.maxMP);
sw.WriteLine("level=" + level);
sw.WriteLine("victories=" + victories);
sw.WriteLine("defeats=" + losses);
sw.WriteLine("tutorial=" + tutorialState);
sw.WriteLine("id=" + userID);
//Write player's attacks
sw.WriteLine("[attack]");
for (int i = 0; i < player.availableAttacks.Count; i++)
{
sw.WriteLine(Convert.ToString(player.availableAttacks[i].id));
}
//Write Demon Lord's pattern
sw.WriteLine("[pattern]");
for (int i = 0; i < enemyPattern.Count; i++)
{
sw.WriteLine(Convert.ToString(enemyPattern[i].id));
}
sw.WriteLine("[end]");
sw.Close();
fs.Close();
}
public void EraseSavedData()
{
tutorialState = 0;
victories = 0;
losses = 0;
level = 0;
FileStream fs = new FileStream("save.dat", FileMode.Create);
StreamWriter sw = new StreamWriter(fs);
sw.WriteLine("[stat]");
sw.WriteLine("id=" + userID);
sw.WriteLine("[end]");
sw.Close();
fs.Close();
Graphics.HideBackground();
Graphics.HideCombatStuff();
Graphics.HideDemonLord();
Graphics.HideLevelUp();
Graphics.HideMoves();
Graphics.HideTitle();
if (tutorialSprite != null) SpriteEngine.EraseGObject(tutorialSprite);
Graphics.EraseCombatLog();
Graphics.EraseTitleStuff();
FirstStart();
}
}
}