-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathScene.java
More file actions
148 lines (124 loc) · 4.67 KB
/
Copy pathScene.java
File metadata and controls
148 lines (124 loc) · 4.67 KB
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
package javagameengine;
import javagameengine.components.Camera;
import javagameengine.components.Component;
import javagameengine.input.Input;
import javagameengine.msc.Debug;
import javagameengine.msc.Vector2;
import javax.swing.*;
import java.awt.*;
import java.util.*;
import java.util.List;
public class Scene extends JPanel {
public LinkedList<Component> layerList = new LinkedList<>();
private ArrayList<Component> components = new ArrayList<>();
private LinkedList<Component> newComponents = new LinkedList<>();
private LinkedList<Component> remove = new LinkedList<>();
Camera camera = new Camera();
public Scene(){
setBackground(new Color(40,125,255));
}
public void instantiate(Component component){
component.start();
newComponents.add(component);
}
public void startScene(){
camera.setScale(new Vector2(1,1));
camera.setPosition(new Vector2(0,0));
}
public void start(){
camera.start();
for(Component c : getComponents1()){
c.start();
}
}
public void add(Component component){
components.add(component);
}
public ArrayList<Component> getComponents1() {
return components;
}
public void setComponents(ArrayList<Component> components) {
this.components = components;
}
public Camera getCamera() {
return camera;
}
public void setCamera(Camera camera) {
this.camera = camera;
}
private float time = 0;
private int lastSec = 0;
private int lastMili = 0;
public Component hasA = null;
public void update(){
time += JavaGameEngine.deltaTime;
if((int) time/100 > lastSec){
lastSec = (int) (time/100);
for(Component component : components) {
component.updateSecund();
}
}
if((int) time/10 > lastMili){
lastMili = (int) (time/10);
for(Component component : components) {
component.updateMili();
}
}
for(Component component : components){
if(inside(component)) {
component.update();
}
}
camera.update();
if(newComponents.size()>0){
components.addAll(newComponents);
newComponents.clear();
}
if(remove.size()>0){
components.removeAll(remove);
remove.clear();
}
}
public void destroy(Component c){
remove.add(c);
}
public boolean inside(Component component){
//Debug.log(component.getPosition().getDistance(JavaGameEngine.getSelectedScene().getCamera().getPosition()));
//return (component.getPosition().getDistance(JavaGameEngine.getSelectedScene().getCamera().getPosition()) < 1500);
//Debug.log(String.valueOf(JavaGameEngine.getSelectedScene().getCamera().getPosition().add(component.getPosition()).getMagnitude()<1000));
return JavaGameEngine.getSelectedScene().getCamera().getPosition().add(component.getPosition()).getMagnitude()<JavaGameEngine.getWindowSize().getMagnitude();
}
@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
Graphics2D graphics2D = (Graphics2D) g;
//graphics2D.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
Vector2 scale = camera.getScale();
//scale = scale.devide(JavaGameEngine.getWindowSize());
graphics2D.scale(scale.getX(),scale.getY());
float width = graphics2D.getClip().getBounds().width/2;
float percentW = 1-scale.getX();
float height = graphics2D.getClip().getBounds().height/2;
float percentH = 1-scale.getY();
graphics2D.translate(width*percentW,height*percentH);
graphics2D.translate(camera.getPosition().getX(),camera.getPosition().getY());
int x = (int) ((int) (Input.getMousePositionOnCanvas().getX() / getCamera().getScale().getX()) + graphics2D.getClip().getBounds().getX());
int y = (int) ((int) (Input.getMousePositionOnCanvas().getY() / getCamera().getScale().getX()) + graphics2D.getClip().getBounds().getY() );
Input.setMousePosition(new Vector2(x,y));
List<Component> list = components;
/*Collections.sort(list, new Comparator<Component>() {
@Override
public int compare(Component o1, Component o2) {
return o1.getLayer() - o2.getLayer();
}
});*/
try{
for(Component c : list){
if(inside(c)) {
(c).render(graphics2D);
}
}
}catch (Exception e){
}
}
}