Hey. So it's been a little while since the last post about this subject, but it's still going strong. In this tutorial, we work a little more using Python with the BGE game Maze Crawl.
So if you haven't been following, Maze Crawl is the little project that I've been making a tutorial about here on Game Up!, and it's gotten pretty good. We have movement and collision. In this part, we'll be adding a couple of things - a better looking Player and a camera system. Now, then, let's take a look at the new Maze Crawl...
Now, then. There's not too much going on for the camera in terms of logic bricks - there's just an Always sensor plugged into a Python controller running the camera script (Game.Camera). The script's pretty simple:
from bge import logic
cont = logic.getCurrentController()
obj = cont.owner
sce = logic.getCurrentScene()
if not 'init' in obj:
obj['init'] = 1
obj['target'] = sce.objects['Player']
obj['depth'] = obj.position.y - obj['target'].position.y
tarpos = obj['target'].worldPosition
pos = obj.worldPosition
soft = .1
obj.position.x += (tarpos.x - pos.x) * soft
obj.position.y += (tarpos.y + obj['depth'] - pos.y) * soft
I should've commented the script moreso, but we'll go through it, and there's nothing here that's too hard to understand. First, we have the camera set its target object in the initialization function. At the beginning, it's set to watch the Player. Also, we get the depth of the camera (how far away from the player the camera is) by subtracting the Player's Y-position from the camera's Y-Position.
After that, we move the camera by adding to the camera's position the difference between the camera's position and the Player's position, taking into account the camera's depth for the Y-axis. Also, we don't do this exactly, but instead move the camera by a fraction of this movement amount - this way, the camera won't snap to the Player's position but will rather 'float' to focus on the Player. The soft variable controls this snappiness.
So, that's the only code running on the camera - the main code difference is in the Player's code. Now, I ran into a bit of a problem making this part of the tutorial series. I had some trouble because part of a game developement tutorial is about making a fun, functional game, but a major part of this particular series is about doing so using Python. What I mean is that I had some trouble figuring out either to go with the Bullet physics engine to produce efficient movement for our character, or to continue doing so purely with Python (handling collision detection, movement, acceleration, friction, etc.). I decided to go about doing this with both methods - utilizing the Bullet physics engine as well as controlling the physics ourselves.