Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implemented critical hits and weapon specific crit chance system #120

Merged
merged 8 commits into from
Jan 20, 2021
8 changes: 7 additions & 1 deletion src/com/hotmail/kalebmarc/textfighter/main/Enemy.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ public class Enemy {
private int coinDropMax;
private int damageMin;
private int damageMax;
int damageDealt;
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Make this a private var, and if you need to access it from another class create a getter for it :)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Alright, I did not even know something like this existed until now :D I have to say that it is much more clever. Still, I posted a question in the commit resolving this issue. I would greatly appreciate if you answered it.

Copy link
Contributor Author

@R1ndT R1ndT Jan 19, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also why can't I just make it a public variable and reference it anywhere? (This is probably common sense for you, but I do not really understand why not)

Copy link
Owner

@hhaslam11 hhaslam11 Jan 20, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

When getting the value, why do I have to type "Weapon.get().getDamageDealt()" instead of just "Weapon.getDamageDealt()", when the variable is not directly tied to the weapon?

Great question! This has to do with how Object-Oriented Programming works. Classes need to be initialized/created before you can use them. Think of them as templates. As I'm sure you're aware, you can have multiple instances of Weapon, each with its own values. The exception is static variables/methods, which is tied to the class itself, instead of the instance of the class. Since getDamageDealt() is not static, that means its tied to the instance and you can not access it directly (even though it changes with each round, it technically is still a property of Weapon).

Weapon.get() is a static method for getting the currently "held" weapon. When calling this, you are actually getting the instance of Weapon, and not Weapon itself

Let me know if this is still confusing, I can write a better example if needed!


Also why can't I just make it a public variable and reference it anywhere?
There's nothing "illegal" about doing it this way, it works just fine, a lot of projects will do it this way just because its easier. Though, it is common (and good) practice to not allow direct access to variables. There are plenty of benefits, including:

  • Security. It can help prevent accidental changes to the variable (you might only want to allow reading the variable, not changing it)
  • You can add validation if needed (using a setter to make sure code isn't trying to set it to a value it shouldn't be)

here's a more technical answer

check this out as well

Note that most of these things usually aren't a problem in smaller projects like this, but become much more important in large projects with dozens of people sharing the same codebase; however, it's always smart to plan ahead and follow good coding styles!

Again, let me know if that still doesn't make sense and I can get better examples for you

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Wow, I did not expect so constructive and comprehensible questions.

I think that I understand for now. I will feel your explanations in practice when programming today and check back here if I need something re-explained.

Thank you so much for the answers, they have really helped me a lot. I am the type of person who does not like doing something he does not understand, so you have essentially made my life a bit easier.

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you so much for the answers, they have really helped me a lot.
No problem! I was in your situation a few years ago, so I definitely understand what it's like; I'm more than happy to help out where I can!

Feel free to reach out whenever, even with questions about your own projects (either code questions, or high-level concepts), or if you just want some code reviews on something! My email is available on my Github profile :)

private int xp;

//Variables
Expand Down Expand Up @@ -109,6 +110,11 @@ void dealDamage() {
Health.takeDamage(damage);
}

void returnDamage(int damage) {
R1ndT marked this conversation as resolved.
Show resolved Hide resolved
damageDealt = damage;
R1ndT marked this conversation as resolved.
Show resolved Hide resolved

}

private void die() {

//Get rewards & store in temp vars
Expand All @@ -118,7 +124,7 @@ private void die() {
com.hotmail.kalebmarc.textfighter.player.Xp.setBattleXp(0, false);

//Prompt enemy death
Ui.popup("You have defeated an enemy! You've found " + tempCoin + " coins, and " + xp + "Xp!", "You've defeated an enemy!", JOptionPane.PLAIN_MESSAGE);
Ui.popup("You have defeated an enemy, dealing " + damageDealt + " damage! You've found " + tempCoin + " coins, and " + xp + "Xp!", "You've defeated an enemy!", JOptionPane.PLAIN_MESSAGE);

//Rewards
testFoundPipe();
Expand Down
32 changes: 29 additions & 3 deletions src/com/hotmail/kalebmarc/textfighter/main/Weapon.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,9 @@ public class Weapon {
public boolean owns;
private int damageMin;
private int damageMax;
private int damageDealt;
private double chanceOfMissing;
private double critChanceMultiplier;
private String name;
private boolean buyable;
//Ammo
Expand All @@ -33,7 +35,7 @@ public class Weapon {
private int ammoIncludedWithPurchase;

public Weapon(String name, int ammoUsed, int ammoIncludedWithPurchase, boolean buyable, int price, //For guns
int ammoPrice, int level, double chanceOfMissing, boolean firstInit, boolean changeDif) {
int ammoPrice, int level, double chanceOfMissing, double critChanceMultiplier, boolean firstInit, boolean changeDif) {

this.name = name;
this.ammoUsed = ammoUsed;
Expand All @@ -43,6 +45,7 @@ public Weapon(String name, int ammoUsed, int ammoIncludedWithPurchase, boolean b
this.ammoPrice = ammoPrice;
this.level = level;
this.chanceOfMissing = chanceOfMissing;
this.critChanceMultiplier = critChanceMultiplier;
this.melee = false;

if (!changeDif) {
Expand Down Expand Up @@ -182,7 +185,6 @@ public int getAmmo() {
}

public void dealDam() {
int damageDealt = 0;

if (this.melee) {
/*
Expand All @@ -200,18 +202,23 @@ public void dealDam() {
if (Random.RInt(100) > this.chanceOfMissing) {
damageDealt += BULLET_DAMAGE;
Stats.bulletsThatHit++;

}

//Results
setAmmo(-1, true);
Stats.bulletsFired += 1;
}
//Run the logic for critical hit
criticalHit();

} else {
noAmmo();
damageDealt = 0;
}
}
//Make damageDealt available in Enemy.java
Enemy.get().returnDamage(damageDealt);

//Display stuff
com.hotmail.kalebmarc.textfighter.player.Stats.totalDamageDealt += damageDealt;
Expand All @@ -230,7 +237,25 @@ public void dealDam() {
if (Enemy.get().getHealth() <= Enemy.get().getHealthMax() / 3){
Enemy.get().useFirstAidKit();
}
}
}
damageDealt = 0;
}

public void criticalHit() {

if (Random.RInt((int) (100 / this.critChanceMultiplier)) == 1) {
R1ndT marked this conversation as resolved.
Show resolved Hide resolved
int critMultiplier = Random.RInt(5, 10);

damageDealt *= critMultiplier;

Ui.cls();
Ui.println("----------------------------------------------------");
Ui.println("Critical Hit!");
Ui.println("You dealt " + critMultiplier + "x normal damage.");
Ui.println("----------------------------------------------------");
Ui.pause();

}
}

public void viewAbout() {
Expand All @@ -248,6 +273,7 @@ public void viewAbout() {
Ui.println("Chance of missing: " + this.chanceOfMissing + "%");
Ui.println("Ammo Used: " + this.ammoUsed);
Ui.println("Damage: " + this.getDamage());
Ui.println("Chance of critical hit: " + this.critChanceMultiplier + "%");
for (int i = 0; i < BORDER_LENGTH; i++) Ui.print("-");//Make line
Ui.pause();
Ui.cls();
Expand Down
28 changes: 17 additions & 11 deletions src/com/hotmail/kalebmarc/textfighter/player/Settings.java
Original file line number Diff line number Diff line change
Expand Up @@ -156,15 +156,18 @@ private static void setConstants(String dif, boolean firstInit, boolean changeDi
//Weapons
//Gun: (name, ammoUsed, ammoIncludedWithPurchase, buyable, price, ammoPrice, level, chanceOfMissing, firstInit, changeDif)
//Melee: (name, startingWeapon, buyable, price, level, damageMin, damageMax, firstInit)

//Melee:
Game.fists = new Weapon("Fists", true, false, 0, 0, 5, 10, firstInit, changeDif);
Game.baseballBat = new Weapon("Baseball Bat", false, true, 120, 1, 10, 15, firstInit, changeDif);
Game.knife = new Weapon("Knife", false, true, 125, 2, 10, 20, firstInit, changeDif);
Game.pipe = new Weapon("Pipe", false, false, 0, 0, 5, 20, firstInit, changeDif);
Game.pistol = new Weapon("Pistol", 1, 18, true, 250, 1, 4, 15, firstInit, changeDif);
Game.smg = new Weapon("Smg", 10, 75, true, 700, 1, 10, 75, firstInit, changeDif);
Game.shotgun = new Weapon("Shotgun", 1, 12, true, 375, 2, 9, 60, firstInit, changeDif);
Game.rifle = new Weapon("Rifle", 1, 18, true, 275, 1, 5, 10, firstInit, changeDif);
Game.sniper = new Weapon("Sniper", 1, 10, true, 700, 2, 7, 0, firstInit, changeDif);
//Guns:
Game.pistol = new Weapon("Pistol", 1, 18, true, 250, 1, 4, 15, 1.5, firstInit, changeDif);
Game.smg = new Weapon("Smg", 10, 75, true, 700, 1, 10, 75, 2.5, firstInit, changeDif);
Game.shotgun = new Weapon("Shotgun", 1, 12, true, 375, 2, 9, 60, 2, firstInit, changeDif);
Game.rifle = new Weapon("Rifle", 1, 18, true, 275, 1, 5, 10, 1.25, firstInit, changeDif);
Game.sniper = new Weapon("Sniper", 1, 10, true, 700, 2, 7, 0, 1, firstInit, changeDif);

//Price
Power.price = 25;
Expand Down Expand Up @@ -198,17 +201,20 @@ private static void setConstants(String dif, boolean firstInit, boolean changeDi
Game.ogre = new Enemy("Ogre", 100, 20, 50, 10, 30, 50, firstInit, changeDif);

//Weapons
//Gun: (name, ammoUsed, ammoIncludedWithPurchase, buyable, price, ammoPrice, level, chanceOfMissing, firstInit, changeDif)
//Gun: (name, ammoUsed, ammoIncludedWithPurchase, buyable, price, ammoPrice, level, chanceOfMissing, critChanceMultiplier, firstInit, changeDif)
//Melee: (name, startingWeapon, buyable, price, level, damageMin, damageMax, firstInit, changeDif)

//Melee:
Game.fists = new Weapon("Fists", true, false, 0, 0, 5, 10, firstInit, changeDif);
Game.baseballBat = new Weapon("Baseball Bat", false, true, 170, 1, 10, 15, firstInit, changeDif);
Game.knife = new Weapon("Knife", false, true, 175, 2, 10, 20, firstInit, changeDif);
Game.pipe = new Weapon("Pipe", false, false, 0, 0, 5, 20, firstInit, changeDif);
Game.pistol = new Weapon("Pistol", 1, 18, true, 275, 1, 4, 15, firstInit, changeDif);
Game.smg = new Weapon("Smg", 10, 75, true, 800, 1, 10, 75, firstInit, changeDif);
Game.shotgun = new Weapon("Shotgun", 1, 12, true, 415, 2, 9, 60, firstInit, changeDif);
Game.rifle = new Weapon("Rifle", 1, 18, true, 300, 1, 5, 10, firstInit, changeDif);
Game.sniper = new Weapon("Sniper", 1, 10, true, 750, 2, 7, 0, firstInit, changeDif);
//Guns:
Game.pistol = new Weapon("Pistol", 1, 18, true, 275, 1, 4, 15, 1.25, firstInit, changeDif);
Game.smg = new Weapon("Smg", 10, 75, true, 800, 1, 10, 75, 1.75, firstInit, changeDif);
Game.shotgun = new Weapon("Shotgun", 1, 12, true, 415, 2, 9, 60, 1.5, firstInit, changeDif);
Game.rifle = new Weapon("Rifle", 1, 18, true, 300, 1, 5, 10, 1, firstInit, changeDif);
Game.sniper = new Weapon("Sniper", 1, 10, true, 750, 2, 7, 0, .75, firstInit, changeDif);

//PRICE
Power.price = 75;
Expand Down