How objects should be in a Java game

EDIT: I just deleted the whole post and reformulated the question as more general.

I want to make a simple strategy game: map, units.

Map: one class. Units: another class, self-sketched.

Simple questions:

  • How the device should be redrawn on the map.
  • The unit must be a JPanel or a similar Swing component (only to control them as an entity using its own mouse manipulators), or it can be another thing, not forgetting that it should be a stand-alone object with its own action of handlers and fields.
  • Is this map model the right simple game that would help me learn in the fun way of Java and OOP.

Here it is!

+4
source share
2 answers

There are two ways.

  • Or you have the Map class, which is the main JPanel, to maintain a collection of units, but save the Unit class as non-Swing. When the map paint () method is called, ask each module to redraw itself by calling the method in each visible module. A module can be inherited from any base class, such as Rectangle, or some other data structure that you use in the program. In this case, the Unit class handles drawing and calculations, but the Map handles clicks and events for each block. He could forward the message to the Division, if necessary; however, if the device itself does not need to know about these things, this method works well. Unit class is lightweight and economical. You can also decide whether the Group knows its position on the map or not.

  • Or each block as a JComponent, and handle its own events separately. The disadvantage is that each instance of the module creates a bunch of data for drawing it. So if you have hundreds of units, only a few of them are drawn, this method is inefficient. The advantage is that each unit can process its own GUI events without translation, etc. It also involves displaying 1: 1 actual units in the game and graphic units on the screen. It is also more difficult to implement certain event handlers in a module if the unit needs to know that other units are around!

  • The third and possibly best way is to have a purely Unit data class containing game information about the device, as in (1), but which has a JComponent creation method when necessary. For example, a JComponent may be an inner class in Unit - it could access Unit data. This is great if the device can be displayed in two places (for example, on two screens or in viewing modes).

-

Adding to Comments

This is true, in (1) Map (main JPanel) implements a mouse handler and each time asks for each unit if it "hit". This allows you to do more complex things, for example, overlap 2 overlays on each other, and both can respond to a click.

In addition, for example, they may not be rectangular or drawn with an alpha channel (if Units were JComponents, they by default capture any mouse event across their entire rectangle). If your blocks do not overlap and are in their own rectangles, then the native JComponent handler is enough.

If you use approach (1), Map can ask each block if it contains a point by clicking, and then Map can handle the selection process. Remember that the device itself will not be able to determine which other units are selected - the choice may include deselecting another device. In this case, the selection operation is a card operation, not a block operation, although it can also change the appearance or function of the device.

If you use separate JComponents for Units, you can override (Point) to determine if the mouse element falls into the element, but this will prevent other units from responding as well. But each module can "choose itself" (set the flag used when drawing), and then notify Map (it will need to find it using getParent or using the specified property).

The key things you need to know before deciding on this project will be: Do you need more than one Map panel per game? Do you need more Unit objects than you need to display? Will you ever need to display the Unit more than once? If the answers are yes, you must separate the Data classes from the View classes, as suggested in 3. then: What does the Unit need to do and what does it need to know about the Map and other units to do this? for example, the move is usually performed by the Map class in this situation, since it depends on the Map and other units; drawing is best done by the Group because there can be many subtypes of units with different data structures that may be required for drawing; Unit selection operations, as you indicated, are somewhere in between. If you see how Swing implements this (for example, ButtonGroup, ListSelectionModel), the "select" itself can be a separate class.

+2
source

I made a couple of solitaire games a couple of years ago, which was pretty much just JPanel, which extended MouseListener and MouseMotionListener. The pegs were primitive circles drawn in the paint method, but you could keep track of where they landed by taking the cursor position and mathematically building the square into which it landed. This is lined up with an array that stores either 1 or 0 for each square on the board, depending on what was there. You can also drag each shape by finding the current cursor position, and then call repaint () in the mouseDragged method that you get from the implementation of MouseMotionListener.

I would suggest that you need to start. Create an array of any size and use it to track units. Then each time you make a move, just check this array and redraw the units in the drawing method. If you use mouse movement, you can get the current position of the device on mouseDown, then do what I mentioned earlier with mouseDragged, and then get your final location on mouseUp, and also do your calculations regarding legal movements, etc. In mouseUp.

In the drawing method, you just go to the array that defines the map, and if there is one at the current position of the array, then do something like g.fillOval (x, y, x_dimension, y_dimension). This array could simply set its elements to 0 without a unit at the current position or 1 for unit 1 of the team at the current position, 2 for team 2, etc. The drawing method will take these numbers and draw the shapes accordingly (change the color for each type or any other).

Hope this makes a little difference.

0
source

Source: https://habr.com/ru/post/1307309/


All Articles