From bdc11380a8126a01a311d76c6fd692eb648202f3 Mon Sep 17 00:00:00 2001 From: Glenn 'devalias' Grant Date: Mon, 12 May 2014 22:40:36 +1000 Subject: [PATCH] Some cleanup and converted to 'object style' Cleaned up some bits and pieces/renamed a couple of things (still not perfect, but better) Converted character and encounter to 'object style'. Added stubs for run, characterDied, monsterDied --- CSS/style.css | 2 +- JS/character.js | 74 ++++--- JS/encounter.js | 183 ++++++++++-------- README.md | 3 + Pics/mace.jpeg => images/mace.jpg | Bin .../sword_and_shield.png | Bin index.html | 33 ++-- js/util.js | 7 + 8 files changed, 180 insertions(+), 122 deletions(-) rename Pics/mace.jpeg => images/mace.jpg (100%) rename Pics/sword_and_shield => images/sword_and_shield.png (100%) create mode 100644 js/util.js diff --git a/CSS/style.css b/CSS/style.css index feb6730..2f760b1 100644 --- a/CSS/style.css +++ b/CSS/style.css @@ -1,2 +1,2 @@ /* Class for keeping divs hidden until JavaScript determines they should be shown. */ -.hidden { visibility: hidden; } \ No newline at end of file +.hidden { display: none; } diff --git a/JS/character.js b/JS/character.js index e6f4f98..d308e2e 100644 --- a/JS/character.js +++ b/JS/character.js @@ -1,33 +1,47 @@ -// Character related variables. -var gold = 0; -var HP = 10; -var lvl = 1; -var XP = 0; -var HP = 10; -var currentHP = 10; -var charname; +var character = { + // Character related variables. + charname: "", + level: 1, + xp: 0, + gold: 0, + maxHP: 10, + currentHP: 10, -// Weapon and hit related variables. -var weapon; -var hit = ["Critical Miss", "Miss", "Glancing Hit", "Hit", "Critical Hit"]; + // Weapon and hit related variables. + weapon: "", + // hit: ["Critical Miss", "Miss", "Glancing Hit", "Hit", "Critical Hit"], // This probably doesnt need to exist here since it's configured in encounter.js -// This function sets the variables charname and weapon with user input from the form on the index page, then starts the game. -function charFill(form) { - window.charname = form.charname.value; - window.weapon = form.weapon.value; - /* These next commands find the divs of the same id and modify their style (CSS) from visible to hidden, or vice versa. - In effect, it controls what the user sees on the page. */ - document.getElementById("charForm").style.visibility="hidden"; - document.getElementById("charInfo").style.visibility="visible"; - document.getElementById("monsterInfo").style.visibility="visible"; - document.getElementById("encounter").style.visibility="visible"; - document.getElementById("charActions").style.visibility="visible"; - // The next two lines call the charLoad and encounter functions. encounter() is in the encounter.js file. - charLoad(); - encounter(); -} + // This function sets the variables charname and weapon with user input from the form on the index page, then starts the game. + charFill: function(form) { + this.charname = form.charname.value; + this.weapon = form.weapon.value; -//This function outputs the starting character info to to the charInfo div. -function charLoad() { - document.getElementById("charInfo").innerHTML = "

Name: " + window.charname + "
" + "Weapon: " + window.weapon + "
" + "Hitpoints: " + currentHP + "/" + HP + "
" + "Level: " + lvl + "
" + "XP: " + XP + "
" + "Gold: " + gold + "

"; -} \ No newline at end of file + /* These next commands find the divs of the same id and modify their style (CSS) from visible to hidden, or vice versa. + In effect, it controls what the user sees on the page. + Note: You could also use visibility="hidden" here, but then it leaves ugly white gaps on your page. */ + document.getElementById("charForm").style.display="none"; + document.getElementById("charInfo").style.display="block"; + document.getElementById("monsterInfo").style.display="block"; + document.getElementById("encounter").style.display="block"; + document.getElementById("charActions").style.display="block"; + + // The next two lines call the charLoad and encounter functions. encounter() is in the encounter.js file. + this.charLoad(); + encounter.encounter(); // This probably shouldn't be called from here.. + }, + + //This function outputs the starting character info to to the charInfo div. + charLoad: function() { + // We split the string assignment up so that it's easier to read (pretty code is good code!) + var charInfo = "

"; + charInfo += "Name: " + this.charname + "
"; + charInfo += "Weapon: " + this.weapon + "
"; + charInfo += "Hitpoints: " + this.currentHP + "/" + this.maxHP + "
"; + charInfo += "Level: " + this.level + "
"; + charInfo += "XP: " + this.xp + "
"; + charInfo += "Gold: " + this.gold; + charInfo += "

"; + + document.getElementById("charInfo").innerHTML = charInfo; + } +}; diff --git a/JS/encounter.js b/JS/encounter.js index 1c0ab34..9039cb0 100644 --- a/JS/encounter.js +++ b/JS/encounter.js @@ -1,82 +1,111 @@ -// Monster related variables. -var monsters = ["rat", "goblin", "zombie", "werewolf", "vampire", "lich"]; -var monster; -var monsterHP; -var monsterDamage; -// Hit related variables. -var hit = ["Critical Miss", "Miss", "Glancing Hit", "Hit", "Critical Hit"]; +var encounter = { + // Monster related variables. + monsters: ["rat", "goblin", "zombie", "werewolf", "vampire", "lich"], + monster: "", + monsterHP: 0, + monsterDamage: 0, -// This function determines what monster the player will be fighting when an encounter begins. -function encounter() { - // This assigns a value from the monsters array by generating a random number between 0 and 5. - window.monster = monsters[Math.floor(Math.random() * 5)]; + // Hit related variables. + hit: ["Critical Miss", "Miss", "Glancing Hit", "Hit", "Critical Hit"], - if (monster == "rat") { - window.monsterHP = 10; - window.monsterDamage = 0; - document.getElementById("encounter").innerHTML = "

You have encountered a " + monster + "!

"; - } - else if (monster == "goblin") { - window.monsterHP = 20; - window.monsterDamage = 1; - document.getElementById("encounter").innerHTML = "

You have encountered a " + monster + "!

"; - } - else if (monster == "werewolf") { - window.monsterHP = 40; - window.monsterDamage = 2; - document.getElementById("encounter").innerHTML = "

You have encountered a " + monster + "!

"; - } - else if (monster == "vampire") { - window.monsterHP = 30; - window.monsterDamage = 3; - document.getElementById("encounter").innerHTML = "

You have encountered a " + monster + "!

"; - } - else { - window.monsterHP = 20; - window.monsterDamage = 4; - document.getElementById("encounter").innerHTML = "

You have encountered a " + monster + "!

"; - } -} + // This function determines what monster the player will be fighting when an encounter begins. + encounter: function() { + // This assigns a value from the monsters array by generating a random number between 0 and 5 (the length of the monsters array minus 1). + var monsterIndex = util.randomIntFromInterval(0, this.monsters.length-1); + this.monster = this.monsters[monsterIndex]; -//This function handles the mechanics of battling monsters. It is far from finished. -function attack() { - //This variable is assigned one of the values from the hit array. This is done by generating a random number between 0 and 5. - var attackRoll = hit[Math.floor(Math.random() * 5)]; - //This variable is used to work out damage, assigned a random number betwen 1 and 6. - var damage = Math.floor((Math.random() * 5) + 1); + if (this.monster == "rat") { + this.monsterHP = 10; + this.monsterDamage = 0; + } + else if (this.monster == "goblin") { + this.monsterHP = 20; + this.monsterDamage = 1; + } + else if (this.monster == "werewolf") { + this.monsterHP = 40; + this.monsterDamage = 2; + } + else if (this.monster == "vampire") { + this.monsterHP = 30; + this.monsterDamage = 3; + } + else { + this.monsterHP = 20; + this.monsterDamage = 4; + } - /* This if tree works out whether or not the attack was a success. - Damage is then assigned appropriately. Critical Miss deals half - damage to the player. Glancing Hit half damage to enemy. Hit is - normal damage to enemy, and Critical double. */ - - /* IMPORTANT TO DOS: - # Make it so that when the monster gets to 0 HP or below, it dies. - # Write a function for the monster's attack. - # Make it so that the player takes damage and can die. - # Assign XP and gold to the player upon killing a monster. - */ - if (attackRoll == "Critical Miss") { - var damage = damage/2; - window.currentHP = currentHP - damage; - document.getElementById("encounter").innerHTML = attackRoll + "! You swing wildly with your " + weapon + " and hit yourself, dealing " + damage + " damage to yourself!"+ "
" + window.monsterHP; - } - else if (attackRoll == "Miss") { - document.getElementById("encounter").innerHTML = attackRoll + "! You lunge sloppily, the " + monster + " easily anticipates your attack and dodges out of the way."+ "
" + window.monsterHP; - } - else if (attackRoll == "Glancing Hit") { - var damage = damage/2; - window.monsterHP = monsterHP - damage; - document.getElementById("encounter").innerHTML = attackRoll + "! Your attack is off balance, and the " + monster + " dodges, but you still nick them, dealing " + damage + " damage." + "
" + window.monsterHP; - } - else if (attackRoll == "Hit") { - window.monsterHP = monsterHP - damage; - document.getElementById("encounter").innerHTML = attackRoll + "! You attack steadily and hit the " + monster + " square on, dealing " + damage + " damage." + "
" + window.monsterHP; - } - else { - var damage = damage*2; - window.monsterHP = monsterHP - damage; - document.getElementById("encounter").innerHTML = attackRoll + "! You attack with expertise, landing a deadly attack upon the " + monster + ", dealing " + damage + " damage." + "
" + window.monsterHP; - } -} \ No newline at end of file + document.getElementById("encounter").innerHTML = "

You have encountered a " + this.monster + "!

"; + }, + + //This function handles the mechanics of battling monsters. It is far from finished. + attack: function() { + //This variable is assigned one of the values from the hit array. This is done by generating a random number between 0 and 5. + var hitIndex = util.randomIntFromInterval(0, this.hit.length-1) + var attackRoll = this.hit[hitIndex]; + //This variable is used to work out damage, assigned a random number betwen 1 and 6. + var damage = util.randomIntFromInterval(1, 6); + + /* This if tree works out whether or not the attack was a success. + Damage is then assigned appropriately. Critical Miss deals half + damage to the player. Glancing Hit half damage to enemy. Hit is + normal damage to enemy, and Critical double. */ + + /* IMPORTANT TO DOS: + # Make it so that when the monster gets to 0 HP or below, it dies. + # Write a function for the monster's attack. + # Make it so that the player takes damage and can die. + # Assign XP and gold to the player upon killing a monster. + */ + if (attackRoll == "Critical Miss") { + damage = damage/2; + character.currentHP = character.currentHP - damage; + document.getElementById("encounter").innerHTML = attackRoll + "! You swing wildly with your " + character.weapon + " and hit yourself, dealing " + damage + " damage to yourself!"+ "
" + this.monsterHP; + } + else if (attackRoll == "Miss") { + document.getElementById("encounter").innerHTML = attackRoll + "! You lunge sloppily, the " + this.monster + " easily anticipates your attack and dodges out of the way."+ "
" + this.monsterHP; + } + else if (attackRoll == "Glancing Hit") { + damage = damage/2; + this.monsterHP = this.monsterHP - damage; + document.getElementById("encounter").innerHTML = attackRoll + "! Your attack is off balance and the " + this.monster + " dodges, but you still nick them, dealing " + damage + " damage." + "
" + this.monsterHP; + } + else if (attackRoll == "Critical Hit") { + damage = damage*2; + this.monsterHP = this.monsterHP - damage; + document.getElementById("encounter").innerHTML = attackRoll + "! You attack with expertise, landing a deadly attack upon the " + this.monster + ", dealing " + damage + " damage." + "
" + this.monsterHP; + } + else { + // Hit + this.monsterHP = this.monsterHP - damage; + document.getElementById("encounter").innerHTML = attackRoll + "! You attack steadily and hit the " + this.monster + " square on, dealing " + damage + " damage." + "
" + this.monsterHP; + } + + // Update the character stats + character.charLoad(); + + // Handle character death + if (character.currentHP <= 0) { + encounter.died(); + } + else if (this.monsterHP <= 0) { + encounter.monsterDied(); + } + }, + + monsterDied: function() { + // TODO: Do something to show that the monster died. + console.log("They dead!"); + }, + + characterDied: function() { + // TODO: Do something to show that the character died. + console.log("You dead!"); + }, + + run: function() { + // TODO: This needs to be implemented. + alert("Oh no! You tried to run away but you hadn't coded that function yet. (Hint: Look in encounter.js for the 'run' function)") + } +}; diff --git a/README.md b/README.md index e69de29..01c5da8 100644 --- a/README.md +++ b/README.md @@ -0,0 +1,3 @@ +# Bestest RPG! + +RPG code for Web Design (WIP) diff --git a/Pics/mace.jpeg b/images/mace.jpg similarity index 100% rename from Pics/mace.jpeg rename to images/mace.jpg diff --git a/Pics/sword_and_shield b/images/sword_and_shield.png similarity index 100% rename from Pics/sword_and_shield rename to images/sword_and_shield.png diff --git a/index.html b/index.html index be95327..34ca451 100644 --- a/index.html +++ b/index.html @@ -1,21 +1,26 @@ The best RPG evah! - + - - + + + +

Bestest RPG!

- +
- Character Name:
- Select Weapon:
+ + + +
@@ -41,24 +46,24 @@ -