/
Swamp.java
171 lines (161 loc) · 6.21 KB
/
Swamp.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
/*
* Copyright (C) 2013-2014 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.example.game.platform.background;
import com.b3dgs.lionengine.Resolution;
import com.b3dgs.lionengine.UtilMath;
import com.b3dgs.lionengine.core.Core;
import com.b3dgs.lionengine.core.Graphic;
import com.b3dgs.lionengine.drawable.Drawable;
import com.b3dgs.lionengine.drawable.Sprite;
import com.b3dgs.lionengine.game.platform.background.BackgroundComponent;
import com.b3dgs.lionengine.game.platform.background.BackgroundElement;
import com.b3dgs.lionengine.game.platform.background.BackgroundPlatform;
import com.b3dgs.lionengine.game.platform.background.Parallax;
/**
* Swamp background implementation.
*
* @author Pierre-Alexandre (contact@b3dgs.com)
*/
final class Swamp
extends BackgroundPlatform
{
/** Moon rasters. */
private static final int MOON_RASTERS = 20;
/** Backdrop. */
private final Backdrop backdrop;
/** Clouds. */
private final Clouds clouds;
/** Parallax. */
private final Parallax parallax;
/** Number of parallax lines. */
private final int parallaxsNumber = 96;
/** The horizontal factor. */
double scaleH;
/** The vertical factor. */
double scaleV;
/**
* Create a rastered element.
*
* @param name The element name.
* @param x The location x.
* @param y The location y.
* @param rastersNumber The number of rasters to use.
* @return The created element.
*/
static ElementRastered createElementRastered(String name, int x, int y, int rastersNumber)
{
final Sprite sprite = Drawable.loadSprite(Core.MEDIA.create(name));
sprite.load(false);
return new ElementRastered(x, y, sprite, rastersNumber);
}
/**
* Constructor.
*
* @param source The resolution source reference.
* @param scaleH The horizontal factor.
* @param scaleV The horizontal factor.
*/
Swamp(Resolution source, double scaleH, double scaleV)
{
super(null, 0, 512);
this.scaleH = scaleH;
this.scaleV = scaleV;
final int width = source.getWidth();
final int halfScreen = (int) (source.getWidth() / 3.5);
this.scaleH = scaleH;
this.scaleV = scaleV;
setOffsetY(source.getHeight() - Scene.NATIVE.getHeight() + 72);
backdrop = new Backdrop(width);
clouds = new Clouds(Core.MEDIA.create("cloud.png"), width, 4);
parallax = new Parallax(source, Core.MEDIA.create("parallax.png"), parallaxsNumber, halfScreen, 124, 50, 100);
add(backdrop);
add(clouds);
add(parallax);
totalHeight = 120;
}
/**
* Backdrop represents the back background plus top background elements.
*
* @author Pierre-Alexandre (contact@b3dgs.com)
*/
private final class Backdrop
implements BackgroundComponent
{
/** Backdrop color. */
private final BackgroundElement backcolor;
/** Mountain element. */
private final BackgroundElement mountain;
/** Moon element. */
private final ElementRastered moon;
/** Mountain sprite. */
private final Sprite mountainSprite;
/** Original offset. */
private final int moonOffset;
/** Screen width. */
int screenWidth;
/** Screen wide value. */
private final int w;
/**
* Constructor.
*
* @param screenWidth The screen width.
*/
Backdrop(int screenWidth)
{
backcolor = createElement("backcolor.png", 0, 0, false);
mountain = createElement("mountain.png", 0, 124, false);
final int x = (int) (208 * scaleH);
moonOffset = 40;
moon = Swamp.createElementRastered("moon.png", x, moonOffset, Swamp.MOON_RASTERS);
mountainSprite = (Sprite) mountain.getRenderable();
this.screenWidth = screenWidth;
w = (int) Math.ceil(screenWidth / (double) ((Sprite) mountain.getRenderable()).getWidthOriginal()) + 1;
}
@Override
public void update(double extrp, int x, int y, double speed)
{
backcolor.setOffsetY(y);
moon.setOffsetY(-20 - moonOffset + getOffsetY());
mountain.setOffsetX(UtilMath.wrapDouble(mountain.getOffsetX() + speed * 0.24, 0.0,
mountainSprite.getWidth()));
mountain.setOffsetY(y);
}
@Override
public void render(Graphic g)
{
// Render back background first
final Sprite sprite = (Sprite) backcolor.getRenderable();
for (int i = 0; i < Math.ceil(screenWidth / (double) sprite.getWidth()); i++)
{
sprite.render(g, backcolor.getMainX() + i * sprite.getWidth(),
(int) (backcolor.getOffsetY() + backcolor.getMainY()));
}
// Render moon
moon.getRaster((int) ((mountain.getOffsetY() + (moonOffset - getOffsetY())) / 4 + Swamp.MOON_RASTERS))
.render(g, moon.getMainX(), (int) (moon.getOffsetY() + moon.getMainY()));
// Render mountains
final int oy = (int) (mountain.getOffsetY() + mountain.getMainY());
final int ox = (int) (-mountain.getOffsetX() + mountain.getMainX());
final int sx = mountainSprite.getWidth();
for (int j = 0; j < w; j++)
{
mountainSprite.render(g, ox + sx * j, oy);
}
}
}
}