Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public partial class Form1 : Form
- {
- private Player _player = new Player(20, 20, 0, 0, 10, 1, 2);
- private Monster _currentMonster;
- private Fight _currentFight;
- public Form1()
- {
- try
- {
- InitializeComponent();
- InitializeStats();
- InitializeEvents();
- _player.MoveTo(World.LocationById(World.location_id_home));
- InitializeButtons();
- }
- catch(Exception ex)
- {
- //MessageBox.Show(ex.Message);
- MessageBox.Show(Convert.ToString(ex));
- }
- }
- private void Form1_Load(object sender, EventArgs e)
- {
- }
- private void InitializeStats() //Fill those Labels!
- {
- lbCurHP.Text = _player.curHP.ToString();
- lbMaxHP.Text = _player.maxHP.ToString();
- lbGold.Text = _player.gold.ToString();
- lbAttack.Text = _player.attack.ToString();
- lbAttack.Text = _player.attack.ToString();
- lbCurExp.Text = _player.curExp.ToString();
- lbMaxExp.Text = _player.maxExp.ToString();
- lbLevel.Text = _player.level.ToString();
- hpBar.Maximum = _player.maxHP;
- hpBar.Value = _player.curHP;
- expBar.Maximum = _player.maxExp;
- expBar.Value = _player.curExp;
- lbTime.Text = World.GetNextTime(1).ToString();
- lbDay.Text = World.GetNextDay(_player).ToString();
- }
- private void UpdateStats()
- {
- lbCurHP.Text = _player.curHP.ToString();
- lbMaxHP.Text = _player.maxHP.ToString();
- lbGold.Text = _player.gold.ToString();
- lbAttack.Text = _player.attack.ToString();
- lbAttack.Text = _player.attack.ToString();
- lbCurExp.Text = _player.curExp.ToString();
- lbMaxExp.Text = _player.maxExp.ToString();
- lbLevel.Text = _player.level.ToString();
- hpBar.Maximum = _player.maxHP;
- hpBar.Value = _player.curHP;
- expBar.Maximum = _player.maxExp;
- expBar.Value = _player.curExp;
- }
- //*****************************//
- //***********EVENTS************//
- //*****************************//
- private void InitializeEvents()
- {
- _player.HpChanged += UpdateHpLabel;
- _player.LocationChanged += UpdateControls;
- _currentFight.MonsterAppear += MonsterAppearOutput;
- _currentMonster.HpChanged += MonsterTakesDamageOutput;
- _player.Victory += MonsterDeadOutput;
- _currentMonster.MonsterAttack += MonsterAttackOutput;
- }
- public void UpdateHpLabel(object sender, PropertyChangedEventArgs e)
- {
- lbCurHP.Text = _player.curHP.ToString();
- hpBar.Value = _player.curHP;
- }
- public void UpdateControls(object sender, PropertyChangedEventArgs e)
- {
- this.Controls.Add(_player.currentLocation.control);
- richTextBox1.Text = _player.currentLocation.description;
- }
- public void MonsterAppearOutput(object sender, EventArgs e)
- {
- richTextBox1.Text = "A wild " + _currentMonster.name + " level " + _currentMonster.level + " appears!\nIt has "+_currentMonster.curHP+" HP.";
- }
- public void MonsterTakesDamageOutput(object sender, PropertyChangedEventArgs e)
- {
- richTextBox1.AppendText("\nYou hit the "+_currentMonster.name+" and dealt "+_player.attack+ " damage.");
- richTextBox1.AppendText("\nThe " + _currentMonster.name + " has " + _currentMonster.curHP + " life remaining.");
- }
- public void MonsterDeadOutput(object sender, EventArgs e)
- {
- richTextBox1.AppendText("\nYou willed the poor " + _currentMonster.name);
- richTextBox1.AppendText("\nYou gained " + _currentMonster.exp + " experience.");
- richTextBox1.AppendText("\nYou picked up " + _currentMonster.gold + " gold.");
- richTextBox1.ScrollToCaret();
- UpdateStats();
- }
- public void MonsterAttackOutput(object sender, EventArgs e)
- {
- richTextBox1.AppendText("\nThe "+_currentMonster.name+ " attacks back and hits you with "+_currentMonster.attack+ " damage.");
- richTextBox1.ScrollToCaret();
- }
- //*****************************//
- //**********BUTTONS************//
- //*****************************//
- private void InitializeButtons()
- {
- World.LocationById(World.location_id_home).control.Controls[0].Click += homeExplore;
- World.LocationById(World.location_id_home).control.Controls[1].Click += homeWait;
- World.LocationById(World.location_id_home).control.Controls[2].Click += homeLocations;
- World.LocationById(World.location_id_home).control.Controls[3].Click += homeSleep;
- World.LocationById(World.location_id_overview).control.Controls[0].Click += overviewBack;
- World.LocationById(World.location_id_overview).control.Controls[1].Click += overviewForest;
- World.LocationById(World.location_id_fight).control.Controls[0].Click += attack;
- World.LocationById(World.location_id_fight).control.Controls[1].Click += run;
- }
- //Home
- private void homeExplore(object sender, EventArgs e)
- {
- if (_player.discoveredForest == false)
- {
- _player.DiscoverForest();
- richTextBox1.Text = "You walk around and found a forest!";
- }
- else
- {
- richTextBox1.Text = "You walk around but there is nothing more to find here...";
- }
- lbTime.Text = World.GetNextTime(1).ToString();
- }
- private void homeWait(object sender, EventArgs e)
- {
- richTextBox1.Text = "You sit around, staring holes into the clouds";
- lbTime.Text = World.GetNextTime(1).ToString();
- _player.Heal(1);
- }
- private void homeLocations(object sender, EventArgs e)
- {
- _player.MoveTo(World.LocationById(World.location_id_overview));
- }
- private void homeSleep(object sender, EventArgs e)
- {
- lbDay.Text = World.GetNextDay(_player).ToString();
- lbTime.Text = World.GetNextTime(1).ToString();
- richTextBox1.Text = "You just slept... Good morning sunshine! A new day is waiting for you!";
- _player.Heal(20);
- }
- //Overview
- private void overviewBack(object sender, EventArgs e)
- {
- _player.MoveTo(World.LocationById(World.location_id_home));
- }
- private void overviewForest(object sender, EventArgs e)
- {
- Random random = new Random();
- int number = random.Next(1, 4);
- switch (number)
- {
- case 1:
- richTextBox1.Text = "You enjoy a peaceful walk in the forest, you feel healthier!";
- _player.Heal(2);
- break;
- case 2:
- richTextBox1.Text = "You trip over a root and scrape your knee.";
- _player.TakeDamage(5);
- break;
- case 3:
- Monster standardMonster = World.MonsterById(World.monster_id_wolf);
- _currentMonster = new Monster(standardMonster.id, standardMonster.name, standardMonster.maxHP, standardMonster.curHP, standardMonster.attack, standardMonster.exp, standardMonster.gold, standardMonster.level);
- _currentFight = new Fight(_player, _currentMonster);
- break;
- case 4:
- richTextBox1.Text = "nothing yet";
- break;
- }
- lbTime.Text = World.GetNextTime(1).ToString();
- }
- //Fight
- private void attack(object sender, EventArgs e)
- {
- _currentFight.FightRound();
- }
- private void run(object sender, EventArgs e)
- {
- Random random = new Random();
- int number = random.Next(1, 4);
- if(number == 1)
- {
- _player.MoveTo(World.LocationById(World.location_id_overview));
- richTextBox1.AppendText("\nBack here again...");
- }
- else
- {
- richTextBox1.AppendText("\nYou couldn't get away!");
- _currentMonster.Attack(_player);
- }
- }
- }
- public class Controls : Panel
- {
- public int id { get; set; }
- public Controls(int id, string name)
- {
- this.id = id;
- this.Name = name;
- Location = new Point(185, 279);
- Size = new Size(377, 75);
- }
- public void AddButton(Button button, string text, bool enabled, bool visible)
- {
- button.Size = new Size(75, 23);
- button.Text = text;
- button.UseVisualStyleBackColor = true;
- button.Enabled = enabled;
- button.Visible = visible;
- int ccount = Controls.Count; //numer of buttons
- int x = ccount * 81 + 3;
- button.Location = new Point(x, 3);
- Controls.Add(button);
- }
- }
- public class Location
- {
- public int id { get; set; }
- public string name { get; set; }
- public string description { get; set; }
- public Controls control { get; set; }
- public Location(int id, string name, string description, Controls control)
- {
- this.id = id;
- this.name = name;
- this.description = description;
- this.control = control;
- }
- }
- public class Monster
- {
- public int id { get; set; }
- public string name { get; set; }
- public int maxHP { get; set; }
- public int curHP { get; set; }
- public int attack { get; set; }
- public int exp { get; set; }
- public int gold { get; set; }
- public int level { get; set; }
- //Events
- public event PropertyChangedEventHandler HpChanged;
- public event EventHandler MonsterAttack;
- public Monster(int id, string name, int maxHP, int curHP, int attack, int exp, int gold, int level)
- {
- this.id = id;
- this.name = name;
- this.maxHP = maxHP;
- this.curHP = curHP;
- this.attack = attack;
- this.exp = exp;
- this.gold = gold;
- this.level = level;
- }
- public void TakeDamage(int damage)
- {
- if (curHP > damage)
- {
- curHP = curHP - damage;
- HpChanged(this, new PropertyChangedEventArgs("curHP"));
- }
- else
- {
- curHP = 0;
- }
- }
- public void Attack(Player player)
- {
- player.TakeDamage(attack);
- MonsterAttack(this, new EventArgs());
- }
- }
- public class Places
- {
- public int id { get; set; }
- public string name { get; set; }
- public Places(int id, string name)
- {
- this.id = id;
- this.name = name;
- }
- }
- public class Player
- {
- public int curHP { get; set; }
- public int maxHP { get; set; }
- public int gold { get; set; }
- public int curExp { get; set; }
- public int maxExp { get; set; }
- public int level { get; set; }
- public int attack { get; set; }
- public Location currentLocation { get; set; }
- //Events
- public event PropertyChangedEventHandler HpChanged;
- public event PropertyChangedEventHandler LocationChanged;
- public event EventHandler Victory;
- //other player-stuff
- public bool discoveredForest { get; set; }
- //only here
- public Player(int curHP, int maxHP, int gold, int curExp, int maxExp, int level, int attack)
- {
- this.curHP = curHP;
- this.maxHP = maxHP;
- this.gold = gold;
- this.curExp = curExp;
- this.maxExp = maxExp;
- this.level = level;
- this.attack = attack;
- }
- public void DiscoverForest()
- {
- discoveredForest = true;
- World.LocationById(World.location_id_home).control.Controls[2].Enabled = true;
- World.LocationById(World.location_id_overview).control.Controls[1].Visible = true;
- }
- public void TakeDamage(int damage)
- {
- if (curHP > damage)
- {
- curHP = curHP - damage;
- }
- else
- {
- curHP = 1;
- Die();
- }
- HpChanged(this, new PropertyChangedEventArgs("curHP"));
- }
- public void Heal(int restore)
- {
- if (curHP < maxHP - restore)
- {
- curHP = curHP + restore;
- }
- else
- {
- curHP = maxHP;
- }
- HpChanged(this, new PropertyChangedEventArgs("curHP"));
- }
- public void MoveTo(Location location)
- {
- if (currentLocation != null)
- {
- currentLocation.control.Visible = false;
- }
- currentLocation = location;
- currentLocation.control.Visible = true;
- LocationChanged(this, new PropertyChangedEventArgs("currentLocation"));
- }
- public void GetRewards(int exp, int gold)
- {
- this.gold += gold;
- if (curExp + exp < maxExp)
- {
- curExp += exp;
- }
- else
- {
- curExp = (curExp + exp) - maxExp;
- LevelUp();
- }
- Victory(this, new EventArgs());
- }
- public void LevelUp()
- {
- level += 1;
- attack += 2;
- maxHP += 5;
- curHP += 5;
- }
- public void Die()
- {
- MoveTo(World.LocationById(World.location_id_home));
- }
- }
- public static class World
- {
- //Lists
- public static readonly List<Monster> monstersList = new List<Monster>();
- public static readonly List<Location> locationsList = new List<Location>();
- public static readonly List<Controls> controlsList = new List<Controls>();
- public static readonly List<Places> placesList = new List<Places>();
- static int time = 6;
- static int day = 0;
- //Monster
- public const int monster_id_wolf = 1;
- public const int monster_id_bee = 2;
- //Locations
- public const int location_id_home = 1;
- public const int location_id_overview = 2;
- public const int location_id_fight = 3;
- //Controls
- public const int control_id_home = 1;
- public const int control_id_overview = 2;
- public const int control_id_fight = 3;
- //Places
- public const int place_id_forest = 1;
- static World()
- {
- PopulateMonsters();
- PopulatePlaces();
- CreateControls();
- PopulateLocations();
- }
- public static int GetNextTime(int hours)
- {
- time += hours;
- if(time > 21)
- {
- LocationById(location_id_home).control.Controls[0].Enabled = false;
- LocationById(location_id_home).control.Controls[1].Enabled = false;
- LocationById(location_id_home).control.Controls[2].Enabled = false;
- LocationById(location_id_home).control.Controls[3].Enabled = true;
- LocationById(location_id_overview).control.Controls[1].Enabled = false;
- }
- return time;
- }
- public static int GetNextDay(Player _player)
- {
- day += 1;
- Player player = _player;
- LocationById(location_id_home).control.Controls[0].Enabled = true;
- LocationById(location_id_home).control.Controls[1].Enabled = true;
- LocationById(location_id_home).control.Controls[3].Enabled = false;
- if (_player.discoveredForest != false)
- {
- LocationById(location_id_home).control.Controls[2].Enabled = true;
- }
- LocationById(location_id_overview).control.Controls[1].Enabled = true;
- time = 6;
- return day;
- }
- private static void PopulateMonsters()
- {
- Monster wolf = new Monster(monster_id_wolf, "Wolf", 10, 10, 1, 10, 2, 1);
- Monster bee = new Monster(monster_id_bee, "Bee", 1, 1, 1, 1, 1, 1);
- monstersList.Add(wolf);
- monstersList.Add(bee);
- }
- private static void PopulateLocations()
- {
- Location home = new Location(location_id_home, "Home", "This is your home", ControlById(control_id_home));
- Location overview = new Location(location_id_overview, "Overview", "The places you discovered", ControlById(control_id_overview));
- Location fight = new Location(location_id_fight, "Fight", "A wild monster appeared: ", ControlById(control_id_fight));
- locationsList.Add(home);
- locationsList.Add(overview);
- locationsList.Add(fight);
- }
- private static void PopulatePlaces()
- {
- Places forest = new Places(place_id_forest, "Forest");
- placesList.Add(forest);
- }
- private static void CreateControls()
- {
- Controls home = new Controls(control_id_home,"home");
- Button explore = new Button();
- Button wait = new Button();
- Button locations = new Button();
- Button sleep = new Button();
- home.AddButton(explore, "Explore", true, true);
- home.AddButton(wait, "Wait", true, true);
- home.AddButton(locations, "Locations", false, true);
- home.AddButton(sleep, "Sleep", false, true);
- Controls overview = new Controls(control_id_overview, "overview");
- Button back = new Button();
- overview.AddButton(back, "Back", true, true);
- foreach(Places place in placesList)
- {
- string name = place.name;
- Button button = new Button();
- overview.AddButton(button, name, true, false);
- }
- Controls fight = new Controls(control_id_fight, "Fight");
- Button attack = new Button();
- Button run = new Button();
- fight.AddButton(attack, "Attack", true, true);
- fight.AddButton(run, "Run", true, true);
- controlsList.Add(home);
- controlsList.Add(overview);
- controlsList.Add(fight);
- }
- public static Controls ControlById(int id)
- {
- foreach(Controls control in controlsList)
- {
- if(control.id == id)
- {
- return control;
- }
- }
- return null;
- }
- public static Location LocationById(int id)
- {
- foreach(Location location in locationsList)
- {
- if(location.id == id)
- {
- return location;
- }
- }
- return null;
- }
- public static Monster MonsterById(int id)
- {
- foreach(Monster monster in monstersList)
- {
- if(monster.id == id)
- {
- return monster;
- }
- }
- return null;
- }
- public static Places PlaceById(int id)
- {
- foreach(Places place in placesList)
- {
- if(place.id == id)
- {
- return place;
- }
- }
- return null;
- }
- }
- public class Fight
- {
- Player player;
- Monster monster;
- //Events
- public event EventHandler MonsterAppear;
- public Fight(Player player, Monster monster)
- {
- this.player = player;
- this.monster = monster;
- player.MoveTo(World.LocationById(World.location_id_fight));
- MonsterAppear(this, new EventArgs());
- }
- public void FightRound()
- {
- monster.TakeDamage(player.attack);
- if (monster.curHP != 0)
- {
- monster.Attack(player);
- }
- else
- {
- player.GetRewards(monster.exp, monster.gold);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement