/
Mario.java
160 lines (147 loc) · 5.72 KB
/
Mario.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
/*
* Copyright (C) 2013-2016 Byron 3D Games Studio (www.b3dgs.com) Pierre-Alexandre (contact@b3dgs.com)
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/
package com.b3dgs.lionengine.tutorials.mario.c;
import com.b3dgs.lionengine.Media;
import com.b3dgs.lionengine.Origin;
import com.b3dgs.lionengine.Updatable;
import com.b3dgs.lionengine.core.Medias;
import com.b3dgs.lionengine.core.awt.Keyboard;
import com.b3dgs.lionengine.drawable.Drawable;
import com.b3dgs.lionengine.drawable.SpriteAnimated;
import com.b3dgs.lionengine.game.Axis;
import com.b3dgs.lionengine.game.Camera;
import com.b3dgs.lionengine.game.Direction;
import com.b3dgs.lionengine.game.Force;
import com.b3dgs.lionengine.game.collision.tile.TileCollidable;
import com.b3dgs.lionengine.game.collision.tile.TileCollidableListener;
import com.b3dgs.lionengine.game.collision.tile.TileCollidableModel;
import com.b3dgs.lionengine.game.object.FramesConfig;
import com.b3dgs.lionengine.game.object.ObjectGame;
import com.b3dgs.lionengine.game.object.Services;
import com.b3dgs.lionengine.game.object.SetupSurface;
import com.b3dgs.lionengine.game.object.trait.body.Body;
import com.b3dgs.lionengine.game.object.trait.body.BodyModel;
import com.b3dgs.lionengine.game.object.trait.mirrorable.Mirrorable;
import com.b3dgs.lionengine.game.object.trait.mirrorable.MirrorableModel;
import com.b3dgs.lionengine.game.object.trait.transformable.Transformable;
import com.b3dgs.lionengine.game.object.trait.transformable.TransformableModel;
import com.b3dgs.lionengine.game.state.StateAnimationBased;
import com.b3dgs.lionengine.game.state.StateFactory;
import com.b3dgs.lionengine.game.state.StateHandler;
import com.b3dgs.lionengine.game.tile.Tile;
import com.b3dgs.lionengine.graphic.Graphic;
import com.b3dgs.lionengine.graphic.Renderable;
/**
* Implementation of our controllable entity.
*/
class Mario extends ObjectGame implements Updatable, Renderable, TileCollidableListener
{
/** Object media. */
public static final Media MEDIA = Medias.create("entity", "Mario.xml");
/** Ground location y. */
private static final int GROUND = 32;
/** Movement force. */
public final Force movement = new Force();
/** Jump force. */
public final Force jump = new Force();
/** Surface. */
public final SpriteAnimated surface;
/** Mirrorable model. */
private final Mirrorable mirrorable = addTrait(new MirrorableModel());
/** Transformable model. */
private final Transformable transformable = addTrait(new TransformableModel());
/** Body model. */
private final Body body = addTrait(new BodyModel());
/** Tile collidable. */
private final TileCollidable tileCollidable = addTrait(new TileCollidableModel());
/** State factory. */
private final StateFactory factory = new StateFactory();
/** State handler. */
private final StateHandler handler = new StateHandler(factory);
/** Camera reference. */
private final Camera camera;
/** Keyboard reference. */
private final Keyboard keyboard;
/**
* Constructor.
*
* @param setup The setup reference.
* @param services The services reference.
*/
public Mario(SetupSurface setup, Services services)
{
super(setup, services);
camera = services.get(Camera.class);
keyboard = services.get(Keyboard.class);
final FramesConfig frames = FramesConfig.imports(setup);
surface = Drawable.loadSpriteAnimated(setup.getSurface(), frames.getHorizontal(), frames.getVertical());
surface.setOrigin(Origin.CENTER_BOTTOM);
surface.setFrameOffsets(-1, 0);
body.setVectors(movement, jump);
body.setDesiredFps(60);
body.setMass(2.0);
}
/**
* Respawn the mario.
*/
public void respawn()
{
transformable.teleport(400, GROUND);
camera.resetInterval(transformable);
jump.setDirection(Direction.ZERO);
body.resetGravity();
}
@Override
protected void onPrepared()
{
StateAnimationBased.Util.loadStates(MarioState.values(), factory, this);
handler.start(MarioState.IDLE);
handler.addInput(keyboard);
}
@Override
public void update(double extrp)
{
handler.update(extrp);
mirrorable.update(extrp);
movement.update(extrp);
jump.update(extrp);
body.update(extrp);
tileCollidable.update(extrp);
if (transformable.getY() < 0)
{
respawn();
}
camera.follow(transformable);
surface.setMirror(mirrorable.getMirror());
surface.update(extrp);
surface.setLocation(camera, transformable);
}
@Override
public void render(Graphic g)
{
surface.render(g);
}
@Override
public void notifyTileCollided(Tile tile, Axis axis)
{
if (Axis.Y == axis && transformable.getY() < transformable.getOldY())
{
body.resetGravity();
}
}
}