Domanda

I would like to use some kind of design pattern but I don't have a good knowledge of the available design patterns

In my race game I would like that people can select a car with different properties (speed, braking, steering input) so that the car they choose for the race will behave differently.

I got this CarHandling class which is used by my Car classs:

// // class CarHandling
public class CarHandling implements ICarHandling {
    private final double ACCELERATION = 100;
    private final double BRAKING = -200;
    private final double MAX_STEERING_INPUT = 0.5;
    ...

}

// class Car constructor
public Car(Point position, ICarHandling carHandling) {
    this.position = position;
    this.carHandling = carHandling;
}

This also includes other images/textures for cars. I thought for storing the car properties (and image locations) to a properties file but don't know how I should load this into my Java Swing game.

È stato utile?

Soluzione

Are you sure you are not over engineering this? I do not think adding a factory here would simplify or make your design any simpler.

It looks like it would be enough to have different kinds of ICarHandling implementations that you would pass to the car constructor when your user selects a different kind of a car.

You could easily extend this design by adding a Textures object that would contain the information about where to load the textures etc. This would not bind certain textures to certain car, and you could mix and match different handlings and textures.

// class Car constructor
public Car(Point position, ICarHandling carHandling, Textures textures) {
    this.position = position;
    this.carHandling = carHandling;
    this.textures = textures;
}

As for the second questions you have

I thought for storing the car properties (and image locations) to a properties file but don't know how I should load this into my Java Swing game.

Assuming you are using somewhat stardard java project structure, store the textures, properties etc in the resources folder, then load them from there.

Really, just google this, there are several easy ways to achieve this with complete code examples.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top