I am currently working on an entity system based game using artemis and libgdx and I am wondering how I should handle level changing in such a configuration.
For example, with a Mario-like platformer:
First Mario starts the level, the engine instantiates a new World() loading the tilemap and initializing all the objects.
What if Mario goes through a pipe? The world inside the pipe is a new World()? or does the camera only focus on another part of a world?
If we have a new world, artemis doesn't allow detaching an entity from a world, so may I clone the player entity and add it to the new world?
Does someone know how to handle this kind of level part changing (Mario's part / Abe's odyssey screens)? What are the common ways to achieve this using an artemis-like framework?
Think about decoupling the building of each particular level so that your world object can take in a specific level that could either read in the position of entities like the mario blocks and pipes or just define them in that specific level class. Allow your world to set new level as a method or you could create a new world object each time and unattach the old one for garbage collection. For something like the world in the pipe if you didn't want to have to load it while the player was already in the level than you could build the room or area somewhere off in the distance and use the pipe to teleport the character to a spot inside the room.
I would create some LevelTagComponent and add it to all entities that are level-specific. Custom Manager should do the rest - turn off some entities when Mario goes into a tunnek or remove them when level is being completely changed.
I finally decided to add a room object layer defining rectangles (see picture below)
Each room is a rectangle with it's x, y coordinate and width, height dimensions. Each room defined the bounds where the camera is allowed to move, if the player intersect with another room, the camera slide from room n to room n+1 along the x axis or the y axis, if the room is bigger than the screen, the camera is allowed to scroll.
I have been able to improve performance and to solve a lot of issues using this approach, it is possible to only process the entities contained within the current room instead of calculating the full world at each frame.
Related
I'm making a 2D (side view) car game with LibGDX using Box2D. The car has a front wheel and a rear wheel and it's "AWD". I would like to render a dust effect to each point where a wheel touches the ground when the player is "burning rubber". To do this, my plan was to
Implement ContactListener, and in beginContact(Contact contact) method find the Contacts where one of the fixtures is ground and the other one a wheel of the car.
Save these Contacts to a (wheel-specific) array, which is supposed to contain the active Contacts of the wheel (the wheel can touch the ground in multiple points, hence an array is required).
During each render call, for each Contact in each wheel's array: Get the contact position, and calculate velocity difference between the wheel body and the ground body at that position. If the length of the velocity difference vector is greater than a certain threshold, then draw the dust effect (using the velocity difference vector to define the speed and angle of the dust particles).
When endContact(Contact contact) of my ContactListener is called, remove contact from the array.
The problem is that LibGDX appears to use the SAME Contact instance for every Box2D contact, rewriting the existing Contact even if it's still active! This means, for example, that if the rear wheel touches the ground first and the front wheel after that, the Contact instance I saved to the rear wheel's contact array gets overwritten with front wheel's contact properties. Another problem is that when I receive endContact, I don't know which Contact really ended.
Have I understood something wrong, or is this how LibGDX is supposed to behave? Is it possible to somehow adjust this behavior, or is there an easy workaround?
(One option that comes to my mind is to keep listening to beginContact and endContact calls and just keep track of the ground bodies/fixtures that are touching each wheel, but then I would have to calculate the contact points manually each frame, which sounds somewhat difficult, expensive, and redundant - because Box2D anyway calculates it at each world step).
Maybe you can create a MyContactListener implementing the ContactListener class.
The MyContactListener class contains a MyContactInventory object which is a Singleton object containing your Contact Array and a couple of convenient method:
addContact
removeContact
The beginContact and endContact add/remove Contacts to your Array using the convenient methods above.
Then you call world.setContactListener( new MyContactListener());
Hope it helps!
I really want to know, is it possible to create a fixture for a body that could be broken by some other body?
There is the example:
a body with its fixture divided into small figures:
and what happens after it is hit by another body:
P.s. Are there any programs that could help the process of creating such fixture?
yes you can do this using Breakable spotted at :
net.dermetfan.gdx.physics.box2d.Breakable
The Breakable allows to easily make whole bodies or single fixtures breakable, which means they will be destroyed if a certain force or friction is applied to them.
How to use
A Breakable is meant to be put in a body's, fixture's or joint's user
data. A single Breakable instance can be put in the user data of
multiple bodies, fixtures and joints. Since this may collide with the
Box2DSprite or other classes using the user data, the
Breakable$Manager uses a Function to access the Breakable in the
user data of a body, fixture or joint.Do not forget to set a Manager
instance as ContactListener to the world and to call destroy() after
every timestep. If the field is already in use, check out the
ContactMultiplexer. The Manager does the actual work, the Breakables
are just passive data holders.
A Breakable consists of a normal resistance, tangent resistance, an
option to destroy its body in case its last fixture was destroyed and
an option setting if the body should be destroyed no matter the amount
of remaining fixtures.
The normalResistance is the force that can be applied to the
Breakable before it breaks (inclusive). The tangentResistance is the
friction the Breakable can bear (also inclusive). The
reactionForceRestiance specifies the reaction force a joint can bear
on each axis. The reactionForceLength2Resistance is the max squared
length of the joint's reaction force the Breakable can bear.
referred to libgdx-utils
some other good references with good examples here and here
for the question (Are there any programs that could help the process
of creating such fixture)
yes you can easily use box2d-editor which allows you to create complex polygons and you can also create your bodies and shapes from your images or sprites check the official documentation in the same page there are several video who explain the way box2d-editor works :
Features:
Automatically decomposes concave shapes into convex polygons,
Automatically traces your images if needed,
Supports multiple outlines for a single body,
Supports polygon and circle shapes,
Reference point location can be changed,
Visual configurable grid with snap-to-grid option,
Built-in collision tester! Throw balls at your body to test it,
Loader provided for LibGDX game framework (written in Java),
Simple export format (JSON), to let you easily create your own loader for any framework in any language.
I have been writing a game in timeline code. I want the different frames (rooms) in the game to be able to share information between each other. Of course, timeline code is limited to the frame it is written in.
After doing quite a bit of reading ("Foundation Game Design with Flash" and a number of articles, tutorials, forums etc) I decided to employ a document class. This did not work either. It seems it only works for frame one but not the rest of the frames (I have four).
How can I have frame four respond to something that happpened in frame one? For example, if the player achieves something in frame one, I want a movie clip in frame four to be visible.
If You are writing your code on the timeline, My suggestion would be to create two layers in the timeline, one for 'frame-actions' - in this layer you insert the code specific to a single frame (will work when the movieclip is stopped on that particular frame).. And also create one more layer called global-actions (for the entire timeline). Only the first frame will be a key frame and there should be empty frames till the end of the timeline.
In this layer actions write the code that you want to access from any keyframe in the same timeline.
If you define a variable in the actions which are written for the whole timeline (global-actions) then that will be available on all the frames.
Now if you want to go to a different frame based on some action, just write some functions in the layer which contains global actions and call that particular function through the frame actions. To go to a different frame use the 'gotoAndStop(frameNumber)' function of flash.
I want to tell you that while it will work, I would not recommend using it in this way.
HTH.
You can use static variables - these are variables which are linked to a class, rather than an instance of it.
Suppose your document class was called Document.as, and you wanted a variable, playerLives, to be visible from any part of the program.
Declare it inside Document.as:
public static var playerLives:int = 3;
You can then reference this directly from anywhere else in your code with:
Document.playerLives
(note that the variable is a member of the class itself, not an instance of it).
You could use a dedicated Statics class to hold these variables if you want to keep your document neat, or attach them to the relevant classes (eg Player.lives)
I've not used timeline/frames for some years but I believe this is how I used to do it!
NB Statics will be fine for your purposes but they are, in some ways, an equivalent to the _global variable in AS2 (at least, they can be used in the same manner) - many would not approve of their use, or over-use, as they are freely accessible from anywhere in your program (thus anathema to the OO concept of encapsulation), but personally I try not to worry about it in small cases - the most important thing to know about the rules of any design pattern is when they can be broken!
They are also slightly slower to access than instance members, but you won't notice this unless you are constantly accessing/changing them (making things like player velocity, which will need to be referenced/changed every frame, static, is not a good idea).
Hope this helps.
You may find the simplest way to link everything with the document class is to move your four frames into a movieclip together and have that on the first frame, then interact with that movieclip.
E.g. in the document class, where the movieclip instance on the timeline is called 'game'.
game.gotoAndStop(4);
game.objectToDisplay.visible = true;
If you encounter reference errors in the IDE then you can avoid these by using [] notation to refer to the properties of game, e.g. game["objectToDisplay"].visible = true;
Note that it's not really best practice to do this, but it will at least help you to finish that first game which is really more important at this stage in your learning. Afterwards, if you want to learn more then I'd recommend "The Essential Guide to Flash Games" by Jeff Fulton from 8bitrocket.com - it will teach you how to use the document class effectively.
I have searched a lot lately, I found that I can load an external swf file to my haxe project at compile time, and use the movieClips via their IDs as Classes types ..
That's cool& nice, but how it would work when I instantiate a MovieClip that has layers?
What I have is MovieClips with layers, layer of image, and a layer of text over the image layer.
So, is this achievable? will I be seeing instances of movieClips (images& texts over them) ?
Let's first dissect the flash terms in terms of code,
Layers :
Consider the layers as grouped z indexes. A single layer with multiple objects will also assign sub z indexes to each clip (see the send to back option in context menu)
The flash IDE provides you a nice interface to group & lock a few instances of objects, while working on the other. Each such group is a layer.
At runtime, every layer (with it's objects) will be concatenated into one single list (the display list) and the objects placed onto the stage in order of their position in the list.
Frames :
Frames are slices of time controlled by the fps property of the flash player. So if you have 12 fps, it means that whatever code you put in that frame of time will receive attention for 1/12th of a second.
Of course there is code being written for anything you do inside a frame or layer. The code, for example that you put in manually in IDE is added in by calling the addframescript internally.
MovieClip
The MovieClip class itself is actually an IDE related thing. The split being Sprite Class plus a timeline (collection of frames) plus associated properties & methods.
The Movieclip class thus provides you with properties like currentFrame, totalFrames, etc & methods like gotoAndPlay(), stop(), etc to interact with the flash controlled class.
Considering all of the above together, you should expect to see different images/text/objects at different intervals of time as defined in the frames when you access the movieclip.
I haven't spent a lot of time in flash, but the answer to your question comes from having a better understanding of how Flash/AS3 works, and not so much to do with Haxe.
Quoting this answer:
Layers only exist in the Flash IDE. They are not part of Flash Player's display list system. So you can't specify what layer a child goes into. Use addChild() or addChildAt() to add children to containers.
So that means Haxe will not have any concept which object is in which layer, nor would AS3 for that matter. The correct approach seems to be to use containers with IDs, which are recognised by AS3 or Haxe, and then add children to the containers.
TLDR: use containers movie clips, not layers.
I have a Flixel project with multiple FlxCamera's. One of them is the main play area, and another is the radar display within the HUD to the right of the main area. I want to add a layer (FlxGroup) to just the radar camera, and I also want to exclude my other layers from the radar camera so they don't randomly show up in the radar's area.
My question is, how do I tell the cameras to only show objects in certain FlxGroup's?
Figured this out on my own. Each object needs to be given a reference to an Array containing references to the FlxCamera objects you want it shown on, and this needs to happen (usually) when the object is first made. The first time a FlxObject calls update(), if its cameras is null, it assigns FlxG.cameras as a default, which means all of the active cameras will display the object.
I did this by making a few static Array's in my Main class, one for each camera group, and then in the constructor for my various classes, I would set their cameras variable to point to the corresponding Array.
The biggest frustration: Currently FlxGroup does not pass its cameras on to its members. Hopefully this will be added into future versions of Flixel so that FlxGroup's can be assigned a camera group and have all their children also automatically assigned the same camera group.