-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGameScreen.java
199 lines (163 loc) · 5.61 KB
/
GameScreen.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
194
195
196
197
198
199
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.Input;
import com.badlogic.gdx.InputMultiplexer;
import com.badlogic.gdx.InputProcessor;
import com.badlogic.gdx.graphics.Color;
import com.badlogic.gdx.graphics.Pixmap;
import com.badlogic.gdx.graphics.Texture;
import com.badlogic.gdx.graphics.g2d.BitmapFont;
import com.badlogic.gdx.scenes.scene2d.Stage;
import com.badlogic.gdx.scenes.scene2d.ui.Label;
import com.badlogic.gdx.scenes.scene2d.ui.Skin;
import com.badlogic.gdx.scenes.scene2d.ui.Table;
import com.badlogic.gdx.scenes.scene2d.utils.Align;
import java.util.ArrayList;
public class GameScreen extends Screen implements InputProcessor {
Map map;
Texture background;
Collider collide;
Player player;
Controller controller;
Stats stats;
Skin skin;
Stage stage;
Label label;
ArrayList<Item> items = new ArrayList<Item>();
public GameScreen(int width, int height) {
super(width, height);
}
public void show() {
super.show();
map = new Map(items);
stats = new Stats();
player = new Player(stats);
controller = new Controller(player);
collide = new Collider(map);
background = new Texture(Gdx.files.internal("sprites/stars.png"));
background.setWrap(Texture.TextureWrap.Repeat, Texture.TextureWrap.Repeat);
setupUI();
InputMultiplexer multi = new InputMultiplexer();
multi.addProcessor(controller);
multi.addProcessor(this);
Gdx.input.setInputProcessor(multi);
}
private void setupUI() {
Table table = new Table();
table.setFillParent(true);
skin = new Skin();
stage = new Stage();
stage.addActor(table);
Pixmap pixmap = new Pixmap(1, 1, Pixmap.Format.RGBA8888);
pixmap.setColor(Color.WHITE);
pixmap.fill();
skin.add("white", new Texture(pixmap));
skin.add("default", new Label.LabelStyle(new BitmapFont(), Color.WHITE));
label = new Label("THIS IS A LABEL", skin);
table.setPosition(50, height - 50);
table.addActor(label);
}
private void setCameraPosition() {
// center camera on player center
float camX = player.getCenterX();
float camY = player.getCenterY();
// clip camera to map edges
if (camX < this.width / 2) {
camX = this.width / 2;
} else if (camX > map.getWidth() - this.width / 2) {
camX = map.getWidth() - this.width / 2;
}
if (camY < this.height / 2) {
camY = this.height / 2;
} else if (camY > map.getHeight() - this.height / 2) {
camY = map.getHeight() - this.height / 2;
}
cam.position.set(camX, camY, 0);
}
@Override
protected void gameStep(float delta) {
player.act(delta);
collide.withMap(player, delta);
player.update(delta);
// deal with all the items
for (int i = items.size() - 1; i >= 0; i--) {
Item item = items.get(i);
if (item.remove) {
items.remove(item);
} else {
item.act(delta);
collide.withMap(item, delta);
collide.entities(player, item);
item.update(delta);
}
}
// mo money
label.setText("" + player.stats.getMoney());
setCameraPosition();
}
@Override
public void render(float delta) {
super.render(delta);
int repeat = 6;
float parallax = cam.position.x * 0.5f;
batch.begin();
batch.draw(background, -512 + parallax, -512, background.getWidth() * repeat, background.getHeight() * repeat, 0, repeat, repeat, 0);
batch.end();
// DEBUG
// map.drawDebug(debug);
// player.drawDebug(debug);
for (Item item : items) {
item.drawDebug(debug);
}
// DEBUG
batch.begin();
map.draw(batch);
player.draw(batch);
// for (Item item : items) {
// item.draw(batch);
// }
batch.end();
stage.draw();
}
@Override
public void resize(int width, int height) {
super.resize(width, height);
}
/**
* INPUT PROCESSOR STUFF
*/
@Override
public boolean keyDown(int i) {
if (i == Input.Keys.R) {
map.populate();
}
return false; //To change body of implemented methods use File | Settings | File Templates.
}
@Override
public boolean keyUp(int i) {
return false; //To change body of implemented methods use File | Settings | File Templates.
}
@Override
public boolean keyTyped(char c) {
return false; //To change body of implemented methods use File | Settings | File Templates.
}
@Override
public boolean touchDown(int i, int i2, int i3, int i4) {
return false; //To change body of implemented methods use File | Settings | File Templates.
}
@Override
public boolean touchUp(int i, int i2, int i3, int i4) {
return false; //To change body of implemented methods use File | Settings | File Templates.
}
@Override
public boolean touchDragged(int i, int i2, int i3) {
return false; //To change body of implemented methods use File | Settings | File Templates.
}
@Override
public boolean mouseMoved(int i, int i2) {
return false; //To change body of implemented methods use File | Settings | File Templates.
}
@Override
public boolean scrolled(int i) {
return false; //To change body of implemented methods use File | Settings | File Templates.
}
}