简体   繁体   English

创建一个简单的游戏引擎

[英]Create a simple, simple, Game Engine

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. 像现在一样,我有一个SurfaceView类和一个Thread类。 The thread-class have this constructor to receive the Game Engine from my SurfaceView-class. 线程类具有此构造函数,可以从我的SurfaceView类接收游戏引擎。 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: 从我的SurfaceView类的surfaceCreated方法中:

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 the 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? GameEngine类的构造函数应如何显示,我应将绘图/计算方法放在哪里? What does the GameEngine? GameEngine是什么?

Now my GameEngine-class look like: 现在我的GameEngine类看起来像:

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. GameEngine类的构造函数应如何显示。

A common pattern is to have a facade class which provides methods to the view. 一种常见的模式是拥有一个提供视图方法的Facade类。 This would be the main controlling class for the UI, so any commands that update the state of the model go through this class. 这将是UI的主要控制类,因此任何更新模型状态的命令都将通过该类。 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. 然后,控制类还将对象从核心传递回用户界面,可以通过getter方法进行访问。 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. 我将在模型中进行的运动,碰撞,AI和东西的计算。 For drawing stuff that depends. 用于绘制依赖的东西。 For greater portability and cohesion I'd generally place that stuff in the view. 为了获得更大的可移植性和凝聚力,我通常将这些东西放在视图中。

Have fun. 玩得开心。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM