/
StateWalk.java
193 lines (177 loc) · 6.12 KB
/
StateWalk.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
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
/*
* 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 java.util.concurrent.atomic.AtomicBoolean;
import com.b3dgs.lionengine.Mirror;
import com.b3dgs.lionengine.anim.Animation;
import com.b3dgs.lionengine.anim.Animator;
import com.b3dgs.lionengine.core.InputDeviceDirectional;
import com.b3dgs.lionengine.game.Axis;
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.object.trait.mirrorable.Mirrorable;
import com.b3dgs.lionengine.game.state.StateGame;
import com.b3dgs.lionengine.game.state.StateInputDirectionalUpdater;
import com.b3dgs.lionengine.game.state.StateTransition;
import com.b3dgs.lionengine.game.state.StateTransitionInputDirectionalChecker;
import com.b3dgs.lionengine.game.tile.Tile;
/**
* Walk state implementation.
*/
class StateWalk extends StateGame implements StateInputDirectionalUpdater, TileCollidableListener
{
/** Horizontal collision. */
private final AtomicBoolean collide = new AtomicBoolean();
/** Movement force. */
private final Force movement;
/** Mirrorable reference. */
private final Mirrorable mirrorable;
/** Animator reference. */
private final Animator animator;
/** Animation reference. */
private final Animation animation;
/** Tile collidable reference. */
private final TileCollidable tileCollidable;
/** Movement side. */
private double side;
/** Played flag. */
private boolean played;
/**
* Create the state.
*
* @param mario The mario reference.
* @param animation The associated animation.
*/
public StateWalk(Mario mario, Animation animation)
{
super(MarioState.WALK);
this.animation = animation;
mirrorable = mario.getTrait(Mirrorable.class);
tileCollidable = mario.getTrait(TileCollidable.class);
animator = mario.surface;
movement = mario.movement;
addTransition(new TransitionWalkToIdle());
addTransition(new TransitionWalkToTurn());
addTransition(new TransitionWalkToJump());
}
@Override
public void enter()
{
movement.setVelocity(0.5);
movement.setSensibility(0.1);
tileCollidable.addListener(this);
side = 0;
played = false;
collide.set(false);
}
@Override
public void exit()
{
tileCollidable.addListener(this);
}
@Override
public void updateInput(InputDeviceDirectional input)
{
side = input.getHorizontalDirection();
}
@Override
public void update(double extrp)
{
if (!played && movement.getDirectionHorizontal() != 0)
{
animator.play(animation);
played = true;
}
movement.setDestination(side * 3, 0);
animator.setAnimSpeed(Math.abs(movement.getDirectionHorizontal()) / 12.0);
if (side < 0 && movement.getDirectionHorizontal() < 0)
{
mirrorable.mirror(Mirror.HORIZONTAL);
}
else if (side > 0 && movement.getDirectionHorizontal() > 0)
{
mirrorable.mirror(Mirror.NONE);
}
}
@Override
public void notifyTileCollided(Tile tile, Axis axis)
{
if (Axis.X == axis)
{
movement.setDirection(Direction.ZERO);
collide.set(true);
}
}
/**
* Transition from {@link StateWalk} to {@link StateIdle}.
*/
private final class TransitionWalkToIdle extends StateTransition implements StateTransitionInputDirectionalChecker
{
/**
* Create the transition.
*/
public TransitionWalkToIdle()
{
super(MarioState.IDLE);
}
@Override
public boolean check(InputDeviceDirectional input)
{
return collide.get() || input.getHorizontalDirection() == 0 && input.getVerticalDirection() == 0;
}
}
/**
* Transition from {@link StateWalk} to {@link StateTurn}.
*/
private final class TransitionWalkToTurn extends StateTransition implements StateTransitionInputDirectionalChecker
{
/**
* Create the transition.
*/
public TransitionWalkToTurn()
{
super(MarioState.TURN);
}
@Override
public boolean check(InputDeviceDirectional input)
{
return input.getHorizontalDirection() < 0 && movement.getDirectionHorizontal() > 0
|| input.getHorizontalDirection() > 0 && movement.getDirectionHorizontal() < 0;
}
}
/**
* Transition from {@link StateWalk} to {@link StateJump}.
*/
private final class TransitionWalkToJump extends StateTransition implements StateTransitionInputDirectionalChecker
{
/**
* Create the transition.
*/
public TransitionWalkToJump()
{
super(MarioState.JUMP);
}
@Override
public boolean check(InputDeviceDirectional input)
{
return input.getVerticalDirection() > 0;
}
}
}