• Announcements

    • khawk

      Download the Game Design and Indie Game Marketing Freebook   07/19/17

      GameDev.net and CRC Press have teamed up to bring a free ebook of content curated from top titles published by CRC Press. The freebook, Practices of Game Design & Indie Game Marketing, includes chapters from The Art of Game Design: A Book of Lenses, A Practical Guide to Indie Game Marketing, and An Architectural Approach to Level Design. The GameDev.net FreeBook is relevant to game designers, developers, and those interested in learning more about the challenges in game development. We know game development can be a tough discipline and business, so we picked several chapters from CRC Press titles that we thought would be of interest to you, the GameDev.net audience, in your journey to design, develop, and market your next game. The free ebook is available through CRC Press by clicking here. The Curated Books The Art of Game Design: A Book of Lenses, Second Edition, by Jesse Schell Presents 100+ sets of questions, or different lenses, for viewing a game’s design, encompassing diverse fields such as psychology, architecture, music, film, software engineering, theme park design, mathematics, anthropology, and more. Written by one of the world's top game designers, this book describes the deepest and most fundamental principles of game design, demonstrating how tactics used in board, card, and athletic games also work in video games. It provides practical instruction on creating world-class games that will be played again and again. View it here. A Practical Guide to Indie Game Marketing, by Joel Dreskin Marketing is an essential but too frequently overlooked or minimized component of the release plan for indie games. A Practical Guide to Indie Game Marketing provides you with the tools needed to build visibility and sell your indie games. With special focus on those developers with small budgets and limited staff and resources, this book is packed with tangible recommendations and techniques that you can put to use immediately. As a seasoned professional of the indie game arena, author Joel Dreskin gives you insight into practical, real-world experiences of marketing numerous successful games and also provides stories of the failures. View it here. An Architectural Approach to Level Design This is one of the first books to integrate architectural and spatial design theory with the field of level design. The book presents architectural techniques and theories for level designers to use in their own work. It connects architecture and level design in different ways that address the practical elements of how designers construct space and the experiential elements of how and why humans interact with this space. Throughout the text, readers learn skills for spatial layout, evoking emotion through gamespaces, and creating better levels through architectural theory. View it here. Learn more and download the ebook by clicking here. Did you know? GameDev.net and CRC Press also recently teamed up to bring GDNet+ Members up to a 20% discount on all CRC Press books. Learn more about this and other benefits here.
Sign in to follow this  
Followers 0
Froggggy

Simple Class Problem

7 posts in this topic

[code]
class Hero
{
public int health, strength, weaponStrength, defence;
public bool isAlive;
public string identity;

public static void Good Guy(Hero hero)
{
hero.health = 25;
hero.strength = 6;
hero.weaponStrength = 15;
hero.defence = 7;
hero.isAlive = true;
hero.identity = "Good Guy";
}
}

class Nemisis
{
public int health, strength, weaponStrength, defence;
public bool isEvil, isAlive;
public string identity;

public static void Bad Guy(Nemesis nem1)
{
nem1.health = 20;
nem1.strength = 10;
nem1.weaponStrength = 10;
nem1.defence = 10;
nem1.isEvil = true;
nem1.isAlive = true;
nem1.identity = "Mean Guy";
}
}

class Caculations
{
public int Calculate()
{
Random numGen = new Random();
double attackValue, defenceValue, reduction;
int damage;

attackValue = (strength * weaponStrength) * (numGen.Next(75, 126) * 0.01);
defenceValue = defence * 2;
reduction = (int)attackValue / defenceValue;
damage = (int)attackValue - (int)reduction;
return damage;
}
}
[/code]

I've got a fairly simple problem in C#. Thank you in advance.

I'm having trouble linking the attributes (strength & weaponStrength) to the calculations to deal the damage in another class that determines the outcome of the battle. I could call the attributes from both the hero and nemesis, but that would require me to write the same formula twice for both classes, is there a better way?
0

Share this post


Link to post
Share on other sites
Even though there is a great difference between a hero and his/hers nemesis in real life, if you look at your code there is (almost) no difference at all. I'd suggest that you create a class Creature/Human/Entity instead and create two separate instances instead.

[CODE]
class Entity {
private int health, strength, weaponStrength, defence;
private bool isAlive, isEvil;
private string identity;
public void Entity(int health, int strength, int weaponStrength, int defence, bool isAlive, bool isEvil, string identity) {
this.health = health;
this.strength = strength;
this.weaponStrength = weaponStrength;
this.defence = defence;
this.isAlive = isAlive;
this.isEvil = isEvil;
this.identity = identity;
}

public int getStrength() {
return this.strength;
}

public Calculate(Entity enemy) {
return this.strength - enemy.getStrength();
}
}

.....
Entity hero = Entity(100, ..., "Good Guy");
Entity nemesis = Entity(100, ..., "Nemesis");
[/CODE]

[i]Heads up, I've never written a single line of code in C# so the syntax may be a little off.. Took a guess from the code you provided. Although the concept should be correct.[/i]

[i]P.S You should be using getters and setters instead of public variables, gives you more control over you code; google "getters and setters C#" and it should be one of the first results. D.S[/i]

[i][b]Edit:[/b] Added a getter for strength.[/i] Edited by Deprecated
1

Share this post


Link to post
Share on other sites
Thanks. I'm haven't learned a great deal in C# as of yet. I'm new to programming, so I haven't come across anything beyond basic class, objects, and methods, so the [color=#0000cd]this[/color] reference is new to me. Looks like I have more reading tonight.
0

Share this post


Link to post
Share on other sites
Oh sorry!

[font=courier new,courier,monospace]this[/font] will refer to the actual instance created from your class. You don't have to use it, but since I used the same names for parameters as instance variables it depends on the implementation of that particular language whether it'll work without [font=courier new,courier,monospace]this[/font] or not.

Think of it like [font=courier new,courier,monospace]the_variables_that_are_defined_on_the_very_top_of_the_class.strength[font=arial,helvetica,sans-serif] , it is the same as when you're using [font=courier new,courier,monospace]numGen[font=arial,helvetica,sans-serif] to call [/font]numGen.Next(...)[/font] but as there is no name for the instance yet you can use the keyword [font=courier new,courier,monospace]this[/font].[/font][/font]
0

Share this post


Link to post
Share on other sites
[quote name='Deprecated' timestamp='1326010955' post='4900578']
[i]P.S You should be using getters and setters instead of public variables, gives you more control over you code; google "getters and setters C#" and it should be one of the first results. D.S[/i]
[/quote]

Or [url="http://msdn.microsoft.com/en-us/library/x9fsa0sw.aspx"]properties[/url] in C#.
[code]
class Entity {
// ......
// ......

public int Strength {
get { return this.strength; }
}
}
[/code]

You then use it as if it was a public variable and not a method. And you can only read from the property if you define only the [b]get [/b]part and not [b]set[/b].
1

Share this post


Link to post
Share on other sites
[quote name='Froggggy' timestamp='1326009932' post='4900576']
[code]
public static void Good Guy(Hero hero)


public static void Bad Guy(Nemesis nem1)
[/code]
[/quote]

Just a note: I assume this is only in your post and not in your actual code, but spaces in identifiers are not allowed (in C# and any other programming language I know of). So this would have to be
[code]
public static void GoodGuy(Hero hero)

public static void BadGuy(Nemesis nem1)
[/code]
0

Share this post


Link to post
Share on other sites
[quote name='Deprecated' timestamp='1326011871' post='4900581']
Oh sorry!

[font=courier new,courier,monospace]this[/font] will refer to the actual instance created from your class. You don't have to use it, but since I used the same names for parameters as instance variables it depends on the implementation of that particular language whether it'll work without [font=courier new,courier,monospace]this[/font] or not.

Think of it like [font=courier new,courier,monospace]the_variables_that_are_defined_on_the_very_top_of_the_class.strength[font=arial,helvetica,sans-serif] , it is the same as when you're using [font=courier new,courier,monospace]numGen[font=arial,helvetica,sans-serif] to call [/font]numGen.Next(...)[/font] but as there is no name for the instance yet you can use the keyword [font=courier new,courier,monospace]this[/font].[/font][/font]
[/quote]

Don't think of it as a variable declared at the top of the class because that's not what it is or how it works, think of it as a reference to the current instance of the object. But you do use it like a normal instance of that object however. It is also only valid within the scope of a class and nowhere else, this matters for languages that aren't purely OO, like C/C++.

Each language that supports this has it's own way of how it is implemented and when you start to think of it as something it is not you can get in to trouble.
0

Share this post


Link to post
Share on other sites
[quote name='brx' timestamp='1326119227' post='4900946']
[quote name='Froggggy' timestamp='1326009932' post='4900576']
[code]
public static void Good Guy(Hero hero)


public static void Bad Guy(Nemesis nem1)
[/code]
[/quote]

Just a note: I assume this is only in your post and not in your actual code, but spaces in identifiers are not allowed (in C# and any other programming language I know of). So this would have to be
[code]
public static void GoodGuy(Hero hero)

public static void BadGuy(Nemesis nem1)
[/code]
[/quote]

It was a simple mistake on my end when posting the topic.
0

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now
Sign in to follow this  
Followers 0