Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Help] How to use ImagineEngine in native development #49

Closed
pmlbrito opened this issue Nov 6, 2017 · 6 comments
Closed

[Help] How to use ImagineEngine in native development #49

pmlbrito opened this issue Nov 6, 2017 · 6 comments
Labels

Comments

@pmlbrito
Copy link

pmlbrito commented Nov 6, 2017

Hi John,
I'm opening this issue after our small twitter ping-pong chat.
I'm here to ask for your help on how to incorporate the ImagineEngine in the context of a native app. I started messing around the playground you created, and then I tried to port it to a full fledged app starting a blank application.
I went through the code and came up that the GameViewController is a (closed) subclass of UIViewController, and that we have to feed it the Scene class where we describe the state of the components, actors and stuff. However (I didn't spend a lot of time into it yet), I couldn't get it to load the playground code in the app as I wasn't able to figure out how to transition between viewcontrollers (is that even possible?). A full fledged app would have more than one gameviewcontroller? Is the game flow represented only by scenes in one gameviewcontroller?

Thanks in advance

@JohnSundell
Copy link
Owner

Hi @pmlbrito 👋

Thanks a lot for opening an issue, it's so much easier than on Twitter 👍 Welcome to Imagine Engine, very exciting that you are trying it out! Since it hasn't been open sourced for long, there's a ton of documentation, guides and tutorials that need to be written, but in the meantime, let me show you how I have setup Imagine Engine in my game Revazendo.

Since Revazendo is a 100% Imagine Engine powered game I use GameWindow to display a GameViewController automatically. Here's what my App Delegate looks like:

@UIApplicationMain class AppDelegate: UIResponder, UIApplicationDelegate {
    var window: UIWindow?

    func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
        let scene = RevazendoScene(size: UIScreen.main.bounds.size)
        let window = GameWindow(scene: scene)
        window.makeKeyAndVisible()
        self.window = window

        return true
    }
}

You could of course just use GameViewController and add it as the rootViewController of a normal UIWindow (that's pretty much what GameWindow does under the hood), or push a GameViewController onto a UINavigationController.

To answer your question about whether to use multiple or a single GameViewController, that's really up to you. You can change the scene of a Game to present a new scene, for example like this:

class MenuScene: Scene {
    func setup() {
        events.clicked.addObserver(self) { scene in
            game?.scene = LevelScene(size: scene.size)
        }
    }
}

Or you could use a UINavigationController to present a new GameViewController. In this regard. Imagine Engine is not very opinionated (in fact, you can just use Game directly and attach its view to any UIView hierarchy if you want). Since the engine is powered by Core Animation you don't pay any penelty for creating multiple Game or Scene instances - you can have many of them running at the same time even if you want - they're all based on CALayers! 😃

Hope the above gives you some insight into how you can setup Imagine Engine in a real app. Let me know if I can help in any other way or answer any other questions - and I'm soon going to post the next tutorial which will be a step by step guide to setting up a cross-platform app using Imagine.

@pmlbrito
Copy link
Author

pmlbrito commented Nov 7, 2017

Hi @JohnSundell

Thank you very much for your fast response. Also, it was very enlightening, as you answered pretty much everything I was thinking about.
In fact, I wasn't thinking as bad as I thought, I was pretty close in understanding how to work with it, but I wasn't trusting it to be that straight forward and easy.
Now I'm wondering why my test didn't work when I tried to add the gameviewcontroller's game view to my viewcontroller as a subview... (I also tried different combinations of uiview sub elements inside the gameviewcontroller)

`class ViewController: UIViewController {

var gameViewController: GameViewController!

override func viewDidLoad() {
    super.viewDidLoad()
  
    let sceneSize = Size(width: 400, height: 400)
    let scene = WalkaboutScene(size: sceneSize)
    self.gameViewController = GameViewController(scene: scene)
    
    self.view.addSubview(self.gameViewController.game.view)
}

}

class WalkaboutScene: Scene {...}`

Anyway, you actually answered a lot more than I asked and gave me a lot of information on how I can continue with my experiments. This is a great project you have here! I'll keep following it actively! Great Work!! And thank you very much for your support!

@JohnSundell
Copy link
Owner

@pmlbrito My pleasure 😄 Happy to help!

To add a child view controller to a UIViewController you need to call addChildViewController(), otherwise it won't receive key events like viewWillAppear. If you want to use your own view controller, you can just use Game directly:

let game = Game(scene: scene)
game.view.frame = view.bounds
view.addSubview(game.view)

That should do it 👍

@JohnSundell
Copy link
Owner

Cool if I close this one @pmlbrito?

@pmlbrito
Copy link
Author

pmlbrito commented Nov 8, 2017

Sorry, didn't have the chance to come here and close it myself. It's cool to close, you've been a great help!

@pmlbrito pmlbrito closed this as completed Nov 8, 2017
@JohnSundell
Copy link
Owner

👍

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants