+ 2
Class structure
HI all, I'm just starting to try to get the concept do the game in my head down into the simple heirechy so far I have Player class that I want to be able to attack () defend (), and move I want the player to be a fleet which is made up of ships also enemy's will be basically a player without the ability to move so do I make player the base class with fleets inheriting from that and in turn ships inheriting from fleets? and do I make enemies a child of Player even though it isn't really a player?
21 odpowiedzi
+ 2
I would suggest making a Command class that has the attack and defend functions in it. then have a child Player class with the move func and any other attributes you would like to define this class. then make a child Enemy class that takes on any attributes you want for it specifically.
This is just another way to think about it and keeps the Player and Enemy classes completely separate from each other.
hope it helps some.
+ 2
I'm going to make 5 different kinds of ships with a variable that holds an int for attack and one for defense I want my players attack to be based on the number of ships and types of those ships so would ships be a child of fleet so then given your post I would have a command class with the children player, enemy, and fleet which has a child ships?
+ 1
I'll make the ships a function of fleets and enemy and player children of fleets I think this will work for me thanks
+ 1
i would make the fleet class separate and make each child ship class of fleet.
you would then need the attack func to call the parent fleet and then child ship for each player/enemy. and each ship would have the attack number and defense number based on size, power, etc.
im not gonna lie kinda passing my realm of expertise. im still learning this stuff, and im sure this is only one way to do it.
+ 1
what language are you writing in?
+ 1
c#
+ 1
thats what i was gonna suggest. im gonna follow this cause im curious how this works out.
+ 1
lol every day I look at this code and see a better way
https://code.sololearn.com/cg80kRMVg2Uj/?ref=app
+ 1
class Unit {
attack() {
console.log("Attack!");
}
}
//inherit this class from Unit
class Gunner
extends Unit{
attack() {
//complete the function
super.attack(); {
console.log("Using gun!");
}
}
}
//inherit this class from Unit
class Sniper extends Unit {
attack(){
//complete the function
super.attack(); {
console.log("Using sniper rifle!");
}
}
}
let gunner = new Gunner();
let sniper = new Sniper();
//calls
gunner.attack();
sniper.attack();
Good Luck
0
great answer ill work it out and hopefully post some code soon
0
I'll be using my unity game engine but I don't think that will change my code drastically
0
here is the beginning of my ships class, obviously I'll be adding a bunch more ship types and I have been starting to think that the fleet class will hold an array of ships () and then return the total defense or attack at the end
0
I'm thinking now that I look closer.at that code I only need to pass in the number of ships because the other stuff is constant
0
its looking good so far.
0
oh wow that link updates the code as I do, very nice solo learn. Anyway I'm just formatting that to run with my unity game engine so it may look weird from here on out
0
ah, that feeling when you work the bugs
0
you code is looking good. any chance you can comment it to help see your thought process
0
yes, sorry that's my bad habit
0
my next project is setting up a battle system, I have already made the controls for flying the spaceship and I think the rest of the help I need falls out of the scope that can be done on this app