-
Notifications
You must be signed in to change notification settings - Fork 6
The Basics of GameObjects
The Basics of GameObjects
Content of this chapter:
A GameObject
is...
- a
ComponentParent
, which means that it can haveComponent
s and that it has aTransform
(-> A Vector2f position,Dimensions
and aRotation
)
-
Drawable
, which means that it has the methoddraw(SaltyGraphics)
, which is called on every repaint for drawing it
- a
FixedTickRoutine
, which mean that it has the methodonFixedTick()
, which is called every fixed tick (e.g. 10ms, look at the basics chapter for more info (!TODO: Write wiki-chapter about the basics!)) and can be used for game logic
-
CollideAble, which means that it has the method
onCollision(CollisionEvent)
, which is called whenever it collides with anotherGameObject
-
InitializeAble, which means that it has the method
initialize()
, which is called once after theGameObject
was added to aScene
. It's important to use this method for as many initialization processes as possible instead of the constructor, because it is called after theScene
is the current one, and after theGameObject
was fully added to theScene
GameObject
has multiple constructors, but all aim to have the Transform
of it, for example like this: float xPos, float yPos, float width, float height
, as well as a tag
. This is a String
and can be used for processing collisions. An example for a tag would be "de.edgelord.superGame.player"
, or just "player"
. It is highly recommended to store those tags as static final Strings
in e.g. a separate class called "GameObjects" or something, so you don't have to use magic values.
Every GameObject
has three default Component
s, these are:
- a
SimplePhysicsComponent
for some simple physics like acceleration and stopping on collision - a
RecalculateHitboxComponent
, which recalculates the position of the defaultHitbox
- a
HitboxCollider
, which detects collisions from the defaultHitbox
Using the default SimplePhysicsComponent
, you can accelerate the GameObject
in a somewhat realistic way, because after the acceleration is gone, the velocity of the GameObject
will slowly fade out.
The SimplePhysicsComponent
is powered by Force
s. A Force
has a name, a Direction
and an acceleration as well as a velocity. You can add Force
s to the SimplePhysicsComponent
using the method addForce()
.
You can also accelerate with GameObject#accelerate(float, Direction)
, and accelerate to a specific velocity directly using GameObject#accelerateTo(float, Direction)
. With the latter, there will be no velocity fade in, nor fade out, which is the better choice for most games. Using this methods, you would have to call them periodically, because their acceleration or velocity (depends on which method you use), is reset each fixed tick. Just experiment a bit to find out the perfect way of using it for your game.
You could try an acceleration of 2500f
and adjust from there.
Direction
is an enum within the class Directions
. There are four values:
Direction.RIGHT
Direction.LEFT
Direction.UP
Direction.DOWN
You can also use these two methods with a Directions
(notice the "s"!), which has a collection of any Direction
s (notice the "s" being outside of the code-block!). Knowing this, making the player control can be very easy, here is a short example:
private final float speed = 2500f;
// ...
@Override
public void onFixedTick() {
accelerateTo(speed, Input.getInput());
}
Input.getInput()
returns an instance of Directions
with all the Direction
s that the player is currently inputting. For more information on that, please read the page about Input
- For
GameObject
s like obstacles, you can set it tostationary
usingsetStationary(boolean)
, which increases the performance noticeably in bigger scenes. The documentation of this feature:
If this is set to true, this GameObject will not have a collision detection. Use this for
stationary objects like obstacles, houses and generally all kind of GameObjects that
aren't moving, due to heavy performance improvements.
Whenever the GameObject#onCollision(CollisionEvent) implementation is empty, set this to true
to improve the performance of the game a lot. Other GameObjects still collide with this one then,
but this one will never collide with others which is redundant when the onCollision(CollisionEvent)
implementation is empty.
Please note that gravity and other forces won't take effect on stationary GameObjects
- You can add
Component
s to a GameObject by usingGameObject#addComponent(Component)
, remove one by callingGameObject#removeComponent(Component)
orGameObject#removeComponent(String)
and get one by its name usingGameObject#getComponent(String)
for more info on that, please read the chapter aboutComponent
s. (!TODO: Wiki chapter about components!)
- every
GameObject
has a mass. This float-value effects the physics. You can set it usingGameObject#setMass(float)
. The default value is1f
.
-
GameObject
has a method calledonCollisionDetectionFinish(List<CollisionEvent>)
, which is called every fixedTick, when the collision detection for thisGameObject
is finished. You can override this method for handling collisions in a more complex way, e.g. for seeing if thisGameObject
is catched between two others. For more info about that, please read the wiki chapter about collision detection and handling (!TODO: Write wiki-chapter about collision detection and handling!)
- you can also move a
GameObject
directly by using the methodsGameObject#move(float, Direction)
,GameObject#moveY(float)
andGameObject#moveX(float)
, but then, it may move into otherGameObjects
, so for in-game moving, please use the method described above.
- if you want to have the
initialize()
method called again at any point, you only have to callGameObject#setInitialized(false)