Question just to clarify my game structure

Started by
4 comments, last by null; 8 years ago

Sorry, I know my questions have been appearing a lot lately - but I think this COULD be the last one for a little while.

I just wanted to know if this structure for damage was good. So, I have a main class, a UI/Graphics class, a player class, and a healthbar class. In my player class, the logic for the health, etc. is applied, and a visual delineation of that logic is displayed in the healthbar class.

If one of the player presses 'j' as the two players are intersecting, damage is done. Same thing with 'f' for the other player. How this works, is in my UI class, I check if the player rectangle belonging the the instance of my player (or my player object, if you will) is intersecting with the player rectangle belonging to the other player.

It goes a little something like this:

NOTE: p1 is my player object, and the '.player' part is referencing the rectangle which my player is drawn to.

This is in my UI/Graphics class:

if(p1.player.intersects(p2.player) {

Player.damageDetected = true //this makes a boolean I have in my player class true, which allows the players to do damage to each other when intersecting and when the appropriate key is pressed

}else{

Player.damageDetected = false;

}

Then in my player class, in the area where key presses are handled, I have something like this:

NOTE: 'k' is the keycode, and e is the keyevent

if(k == e.VK_J && damageDetected = true) {

do damage, etc, etc.

}

My question is: Is this structure correct? This isn't EXACTLY how the code looks, but it's pretty much the same idea.

Thank you!

Advertisement

For startes, there's no "correct" way to do something most things. As a game developer there are three things you want:

  • stable code (meaning no crashes)
  • [decently] fast code (meaning it hits your FPS target)
  • and good gameplay (meaning your game is responsive and fun to play)

How you go about it, is in many cases irrelevant. Unreal Engine is one of the more popular game engines right now and just look at the treatment it gets when you brush away the flashy top soil. Despite its popularity it's not their way or the highway. And it will never be :).

That being said, what you really want to achieve is for your code to do what you want it to do. If it does that and the above three points are covered, then move on. Come back to it if you feel it's not quite what you want or you get a better idea how to do it. If the code doesn't do what you need it to, then analyze it further and try to figure out why it doesn't do that. Notice that the stress here is on what you want it to do :). That's because it's your game and the question you posed cannot really be answered by anyone else.

I could make remarks about the redundant comment and the superfluous else case, or the fact that you call a rectangle your player, who is a rectangle (which might be perfectly okay depending on what your game is - especially if your player is a rectangle) or the fact that you have a typo on the line "if(k == e.VK_J && damageDetected = true)". But at the end of the day none of that would be answering your question :).

There's really nothing wrong with how it's implemented. But why is the collision test in with the UI logic? The collision test is not UI logic, it's game logic, so it should be put in a class intended to manage the overall game state.

For startes, there's no "correct" way to do something most things. As a game developer there are three things you want:

  • stable code (meaning no crashes)
  • [decently] fast code (meaning it hits your FPS target)
  • and good gameplay (meaning your game is responsive and fun to play)

How you go about it, is in many cases irrelevant. Unreal Engine is one of the more popular game engines right now and just look at the treatment it gets when you brush away the flashy top soil. Despite its popularity it's not their way or the highway. And it will never be :).

That being said, what you really want to achieve is for your code to do what you want it to do. If it does that and the above three points are covered, then move on. Come back to it if you feel it's not quite what you want or you get a better idea how to do it. If the code doesn't do what you need it to, then analyze it further and try to figure out why it doesn't do that. Notice that the stress here is on what you want it to do :). That's because it's your game and the question you posed cannot really be answered by anyone else.

I could make remarks about the redundant comment and the superfluous else case, or the fact that you call a rectangle your player, who is a rectangle (which might be perfectly okay depending on what your game is - especially if your player is a rectangle) or the fact that you have a typo on the line "if(k == e.VK_J && damageDetected = true)". But at the end of the day none of that would be answering your question :).

Wow I just saw that typo XD As I said, this isn't like a copy - paste, it's just the general way I went about it. And the only reason I have that else case in there was because - well, I don't really know. I know it's redundant, but I do it like that because why not :P

Again, I'm drawing the actual graphics to a rectangle which is my player - I called it the player for name's sake and I couldn't think of a better name for it.

Thanks for the clarification! I just want everything to be efficient.

Why are you detecting collision with your UI object? Even if you have a simple 2d game where you only are checking 2d box collision, you should still have a separate system/module/class specifically for collision and other "physics" tasks.

Why are you detecting collision with your UI object? Even if you have a simple 2d game where you only are checking 2d box collision, you should still have a separate system/module/class specifically for collision and other "physics" tasks.

Yeah I realized and I fixed that. I have a GameState class now, along with a HealthBar class (just to let you know).

This topic is closed to new replies.

Advertisement