-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathScreen.java
144 lines (116 loc) · 4.04 KB
/
Screen.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
package uet.oop.bomberman.graphics;
import uet.oop.bomberman.Board;
import uet.oop.bomberman.Game;
import uet.oop.bomberman.entities.Entity;
import uet.oop.bomberman.entities.character.Bomber;
import java.awt.*;
/**
* Xử lý render cho tất cả Entity và một số màn hình phụ ra Game Panel
*/
public class Screen {
protected int _width, _height;
public int[] _pixels;
private int _transparentColor = 0xffff00ff;
public static int xOffset = 0, yOffset = 0;
public Screen(int width, int height) {
_width = width;
_height = height;
_pixels = new int[width * height];
}
public void clear() {
for (int i = 0; i < _pixels.length; i++) {
_pixels[i] = 0;
}
}
public void renderEntity(int xp, int yp, Entity entity) { //save entity pixels
xp -= xOffset;
yp -= yOffset;
for (int y = 0; y < entity.getSprite().getSize(); y++) {
int ya = y + yp; //add offset
for (int x = 0; x < entity.getSprite().getSize(); x++) {
int xa = x + xp; //add offset
if(xa < -entity.getSprite().getSize() || xa >= _width || ya < 0 || ya >= _height) break; //fix black margins
if(xa < 0) xa = 0; //start at 0 from left
int color = entity.getSprite().getPixel(x + y * entity.getSprite().getSize());
if(color != _transparentColor) _pixels[xa + ya * _width] = color;
}
}
}
public void renderEntityWithBelowSprite(int xp, int yp, Entity entity, Sprite below) {
xp -= xOffset;
yp -= yOffset;
for (int y = 0; y < entity.getSprite().getSize(); y++) {
int ya = y + yp;
for (int x = 0; x < entity.getSprite().getSize(); x++) {
int xa = x + xp;
if(xa < -entity.getSprite().getSize() || xa >= _width || ya < 0 || ya >= _height) break; //fix black margins
if(xa < 0) xa = 0;
int color = entity.getSprite().getPixel(x + y * entity.getSprite().getSize());
if(color != _transparentColor)
_pixels[xa + ya * _width] = color;
else
_pixels[xa + ya * _width] = below.getPixel(x + y * below.getSize());
}
}
}
public static void setOffset(int xO, int yO) {
xOffset = xO;
yOffset = yO;
}
public static int calculateXOffset(Board board, Bomber bomber) {
if(bomber == null) return 0;
int temp = xOffset;
double BomberX = bomber.getX() / 16;
double complement = 0.5;
int firstBreakpoint = board.getWidth() / 4;
int lastBreakpoint = board.getWidth() - firstBreakpoint;
if( BomberX > firstBreakpoint + complement && BomberX < lastBreakpoint - complement) {
temp = (int)bomber.getX() - (Game.WIDTH / 2);
}
return temp;
}
public void drawEndGame(Graphics g, int points) {
g.setColor(Color.black);
g.fillRect(0, 0, getRealWidth(), getRealHeight());
Font font = new Font("Arial", Font.PLAIN, 20 * Game.SCALE);
g.setFont(font);
g.setColor(Color.white);
drawCenteredString("GAME OVER", getRealWidth(), getRealHeight(), g);
font = new Font("Arial", Font.PLAIN, 10 * Game.SCALE);
g.setFont(font);
g.setColor(Color.yellow);
drawCenteredString("POINTS: " + points, getRealWidth(), getRealHeight() + (Game.TILES_SIZE * 2) * Game.SCALE, g);
}
public void drawChangeLevel(Graphics g, int level) {
g.setColor(Color.black);
g.fillRect(0, 0, getRealWidth(), getRealHeight());
Font font = new Font("Arial", Font.PLAIN, 20 * Game.SCALE);
g.setFont(font);
g.setColor(Color.white);
drawCenteredString("LEVEL " + level, getRealWidth(), getRealHeight(), g);
}
public void drawPaused(Graphics g) {
Font font = new Font("Arial", Font.PLAIN, 20 * Game.SCALE);
g.setFont(font);
g.setColor(Color.white);
drawCenteredString("PAUSED", getRealWidth(), getRealHeight(), g);
}
public void drawCenteredString(String s, int w, int h, Graphics g) {
FontMetrics fm = g.getFontMetrics();
int x = (w - fm.stringWidth(s)) / 2;
int y = (fm.getAscent() + (h - (fm.getAscent() + fm.getDescent())) / 2);
g.drawString(s, x, y);
}
public int getWidth() {
return _width;
}
public int getHeight() {
return _height;
}
public int getRealWidth() {
return _width * Game.SCALE;
}
public int getRealHeight() {
return _height * Game.SCALE;
}
}