I'm on my way with my first game, all design and that stuff are done, just coding left. I have successfully watched tutorials around the world wide web with information about Graphics and how to create a successfully thread-synchronization within it.
Like now I have a SurfaceView-class and a Thread-class. The thread-class have this constructor to receive the Game Engine from my SurfaceView-class. Simple code from Thread-class constructor:
public Thread(SurfaceHolder localsurfaceHolder,
Context context,
Handler handler,
SurfacefView localEngine) {
surfaceHolder = localsurfaceHolder;
this.handler = handler;
this.context = context;
engine = localEngine;
}
From my SurfaceView-class in the surfaceCreated-method:
thread = new GameThread(getHolder(), context, new Handler(), this);
And there is my code that will start trigging the thread when the surface is created.
Now to the real deal. Shortly I want a game engine that is totally separated from 开发者_如何学JAVAthe view. The problem is; how to achieve this? How should the GameEngine-class constructor look like and where shall I put the drawing/calculations-methods? What does the GameEngine?
Now my GameEngine-class look like:
package com.myname.mygame;
public class GameEngine {
//If im right, a constructor for the engine should be placed here
//How and which parameters shall I change and send with from the view
}
Thanks in advance!
How should the GameEngine-class constructor look like.
A common pattern is to have a facade class which provides methods to the view. This would be the main controlling class for the UI, so any commands that update the state of the model go through this class. Since you generally only have need for one instance a singleton can be useful here.
public class GameEngine {
private static GameEngine instance = null;
private GameEngine(){
// init stuff here
}
public static GameEngine getInstance(){
if(instance == null){
instance = new GameEngine();
}
return instance;
}
}
The controlling class would then also pass objects from the core back to the userinterface, which can be accessed via getter methods. I'm not saying this is how you must do it, its just a way of going about things with loose coupling.
where shall I put the drawing/calculations-methods?
Calculations for movement, collisions, AI and stuff I'd put in the model. For drawing stuff that depends. For greater portability and cohesion I'd generally place that stuff in the view.
Have fun.
精彩评论