What happens to game enemies once they leave the screen?
This is a question that has been buzzing at my ear for about a year, and I’ve recently found an answer.
It’s one thing to create a bunch of on-screen enemies and make them interact with the player, but to make the game more challenging and enjoyable, they really ought to have their own view of the game world, independent of the player’s screen.
This means duplicating the collision detection and the scrolling engine in virtual terms, for each enemy.
For my object based game engine, there’s quite a lot of processing that needs to be done each frame just for collision detection and scrolling, so the idea of duplicating those processes for each and every enemy seemed very daunting, and I wouldn’t have approached it in AS2 at all.
Hey, AS3 is 10 times faster right, so…
2 Player Split Screen with MVC
First thing was to make a multi-view system by implementing an MVC design pattern throughout the engine. This means there’s only one World state (known as the Model), which is basically just a load of maps built from WorldObjects like platforms and obstacles. Then for each player a distinct View is created that is responsible for performing collision detection for the player and scrolling the map around the screen (or rather the screen around the map).
This wasn’t too hard to implement really, it took a weekend to rebuild the foundation of the engine, with the biggest challenge being the management of communication between the state of every WorldObject and it’s visual counterpart in the View (a WorldObjectDisplay).
Even this was quite straightforward – when an object is flagged as part of the player’s view, a WorldObjectDisplay is created with a reference to the WorldObject, then the WorldObjectDisplay adds a listener to the WorldObject and just waits for updateDisplay messages. The WorldObject knows nothing of its (any number of) displays; it just looks after it’s own business and dispatches an event when something interesting happens. Pretty easy!
With this implemented, it was just a simple case of:
var player = new Player(“P1”);
player.createView(0, 0, 400, 400); // x, y, width, height
var player2 = new Player(“P2”);
player2.createView(400, 0, 400, 400); // x, y, width, height
and hey presto there’s a split screen game:
Persistent AI with Virtual View
The hard stuff required for persistent AI is covered by having a multi-view system using MVC. All that remains is to create an AbstractView class that doesn’t have any code responsible for moving actual MovieClips around, and doesn’t bother with managing a player’s input, it just keeps a track of the position within the map and collision detection with any WorldObject’s coordinates.
Then it’s just a case of subclassing one of these virtual views for an enemy and sticking some simple A.I. into the enemy class:
These enemy worms will collide with the terrain, but they’ll be doing it virtually, and will continue to collide outside of the player’s view.
The worms don’t exist visually in their own view at all, they only exist visually when the player encounters them.
Schrodinger’s Worms yo!
4 Comments
Christopher J. Rock
Is this the new post you were talking about?
I think it’s a good idea, though I’ve been programming with a different perspective lately.
Having your bad guys interacting with the world even when they’re off-screen is a good way to keep them “alive.” They can appear to have a mind of their own in that the player sees events will continue whether or not they move forward in the game. I think that’s important to a game’s ability to earn the player’s respect. It has to feel as though the world is alive and operating on its own, an equal to the player in terms of free will. Of course, programming a game to actually have free will would be tough, but tricking the player into thinking so, just for a little while, isn’t so tough.
As far as actual program design, I first had an engine with the same dilemma as yours. It was all based on hitTesting so I had to use spawning positions for bad guys because there was no way I could run that many hitTests without crashing things. But I’ve been revamping things and I’m hoping to see some good results.
I’m switching everything over to straight math instead of hitTests. The idea is that hitTesting is way too slow, so instead I keep track of moving points and edges and calculate when their paths will cross. If they cross, we have a collision and react accordingly.
It’s a complete mystery how much flash will be able to handle, but I expect it to be a lot more than before and the calculations will give me much more accurate and informative results too.
As far as split screen, I’ve been wondering about using actionscript to draw out art and attach movieclips, update their locations etc. Basically, treating art as totally separate from the “hypothetical” world that the code is simulating. Then I’d figure you could get as many cameras running as flash can handle. But I’m just talking out of my ass because I haven’t even gotten a chance to try split screen yet. . . .
Actionscript 3 is definitely the way to go. Nice work jumping right into it. I haven’t even bothered yet. . . . Fortunately what I’m coding lately will be easy to translate, but man, I’m not in the mood to learn a whole new grammar just yet. . . .
Are you gonna upload a demo of this stuff?
James @ FuturLab
Hey Chris,
Yeah, this is the one. I thought you may have had a different perspective to how I’ve done it, but happy to see that you agree at least in theory that this is the way to go.
Performing collision detection just using math on abstract data is definitely more flexible.
I’ve had a split screen game with two persistent AI running at the same time, and it’s been fine speed wise.
So, that’s 4 collision detection and scrolling engines running at the same time – with two of the scrolling engines running several layers of parallax, and the other two performing some pretty nifty path finding AI with line/line collision.
All this runs without noticeable slowdown!
I’ll put it online when it’s ready 🙂
Anonymous
James,
Just trying to kill some time, flicking through this bloggin lark. Had quite a chuckle to myself, haven’t got the slightest idea what you are talking about ya nerd!!
Loved the commments about learn some manners, and say hello before launching etc etc. Brilliant, nicely put.
Hope things are cool, bro back yet?
All the best
Connor.
:CPO RN – VD BAR STAR
Defending the nation
Proud to serve
Living the dream
(so i’ve been told)
Anonymous
Electronic memory is something that I seemingly will never have enough of. It's as if megabytes and gigabytes have become an inseparable part of my day to day existence. Ever since I bought a Micro SD Card for my DS flash card, I've been on the constant lookout for large memory at low prices. I feel like I'm going insane.
(Submitted using SKu2 for R4i Nintendo DS.)