-2
// in general, it could be potion, armor, weapon, etc.
Item class {
   // states
   id
   name
   // behavior
   set, get of states here..

   use()
   // still thinking what is the other general behavior
} 

// in general, it could be Gun,Sword,Wand, Bow (range,melee,magic)
Weapon class extends Item{
   // gun - fire(); type: range (in the movie, the gun can be use as melee weapon too xD)
   // sword - slash(); type: melee (in the movie, the sword can be thrown :) or use as magic wand?)
   // wand - ??
   // mace - bash();
   // bow - shoot() ??
   // so what is the general behavior of this weapon?
} 

Bag class {} // parent is Item, collection of items here

// Human, Orc, etc
Character class {
   // states
   Weapon

   // behavior
   attack();
   // still thinking what is the other behavior and states
} 

// Swordsman, Magician, Archer, etc.
Hero class extends Character{
} 

The above code is my OOP classes of Role Playing Game that I am developing, and I'm having hard time thinking of the general states and behavior of each classes.

Problem

  • How do I create these classes (I am beginner in OOP). I've heard some encapsulations, polymorphish etc.
  • I don't know how to use properly the interface, abstract etc.
  • Thinking of the general states and behavior of classes
  • As you can see in the weapon class. The gun, sword, bow can be use as melee, range, or even magic. And should I call these WeaponType? (range,melee,magic)
mike
  • 1,233
  • 1
  • 15
  • 36
ronscript
  • 397
  • 1
  • 8
  • 33
  • 1
    Side note it's `class Foo` not `Foo class` Maybe you could start by reading some tutorial, like https://docs.oracle.com/javase/tutorial/java/index.html, this question is too broad as is –  Jul 08 '16 at 11:37
  • Sure you got the correct tag? This is for sure not Java source code?! – GhostCat Jul 08 '16 at 11:39
  • just a typo, sorry. – ronscript Jul 08 '16 at 11:40
  • 1
    Besides: your question is **way too broad**. You basically dropped a lot of requirements and ideas, and then you ask us to explain to you how to do all of that. Sorry, that is **not** how stackoverflow works. The one general answer I got for you: search for "Agile principles Robert Martin pdf"; download that, and read it. – GhostCat Jul 08 '16 at 11:40
  • I fail to see how "weapon" can be a state. – Pieter De Bie Jul 08 '16 at 11:41
  • sorry, its not literally a states. What I mean by that is class fields – ronscript Jul 08 '16 at 11:42
  • Your mistake is to create the classes before you modelled what you want in the end. Classes do not exist for themselves but in order to achieve a goal. Do this: make a version of your game on paper alone, without any reference to programming. Collect what you need, like status effects and the like. Play it on paper (with some friend) and make the notes you need to do it as simple and informative as possible. Then think about what "types" of notes you used. Those are most likely your classes. Do not do more. More is not better. For example, I see no reason why (to be continued) – Aziuth Jul 08 '16 at 11:48
  • (continuation) why you need a separate class for each weapon type or why you need to model weapon type anyways. If they do not behave differently from a programmers view, they are the same class. – Aziuth Jul 08 '16 at 11:49

2 Answers2

2

Let's answer your queries line by line

How do I create these classes (I am beginner in OOP). I've heard some encapsulations, polymorphish etc.

You cannot develop a game without having a through understand of OOPS.Spend some time understanding Abstraction/Encapsulation/Polymerphsim/Inheritance.

I don't know how to use properly the interface, abstract etc.

Maybe if you know what those mean you would be able to decide when and how to use them if at all you would be needing them.

Thinking of the general states and behavior of classes As you can see in the weapon class. The gun, sword, bow can be use as melee, range, or even magic. And should I call these WeaponType? (range,melee,magic)

In this case,you can have an interface called Weapons with their types as abstract methods.You can inherit them and make the weapon property changes for each weapon etc like attack power,strength etc.Of course again you need to be through with OOPS concepts. If you're a quick learner Start by watching this Video.

Technologeek
  • 190
  • 1
  • 2
  • 13
0

For a complex design like a game I suggest you to study better interfaces and abstract classes. Then I suggest you to draw an UML diagram of the model of your game. Weapon can be an Interface with a fire() method and all the weapons that implements the interface will have their own fire() method.

For a good design I also suggest to learn design patterns very well. If you do a good design of your game, in the future will be very easy extend your game with new features!

frankjust
  • 19
  • 9