-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathWorld.java
More file actions
112 lines (101 loc) · 3.47 KB
/
Copy pathWorld.java
File metadata and controls
112 lines (101 loc) · 3.47 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
package GameObjects;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.util.ArrayList;
/**
* Created by Shadilan on 07.10.2015.
*/
public class World {
private int CenterX;
private int CenterY;
private int VisibleWidth;
private int VisibleHeight;
private int Height;
private int Width;
private ArrayList<GameObject> Objects;
private ArrayList<GameObject> ObjToAdd;
private ArrayList<GameObject> ObjToRem;
public World(int width, int height,int visibleWidth,int visibleHeight,int centerX,int centerY){
Width=width;
Height=height;
VisibleHeight=visibleHeight;
VisibleWidth=visibleWidth;
CenterX=centerX;
CenterY=centerY;
ObjToAdd=new ArrayList<GameObject>();
ObjToRem=new ArrayList<GameObject>();
Objects=new ArrayList<GameObject>();
}
public World(int width, int height,int visibleWidth,int visibleHeight){
Width=width;
Height=height;
VisibleHeight=visibleHeight;
VisibleWidth=visibleWidth;
CenterX=width/2;
CenterY=height/2;
ObjToAdd=new ArrayList<GameObject>();
ObjToRem=new ArrayList<GameObject>();
Objects=new ArrayList<GameObject>();
}
public void SetCenter(int x,int y){
CenterY=y;
CenterX=x;
}
public void AddObject(GameObject obj){
ObjToAdd.add(obj);
}
public void RemoveObject(GameObject obj){
ObjToRem.add(obj);
}
public void Draw(Canvas canvas){
Image img=canvas.createImage(VisibleWidth,VisibleHeight);
Graphics bufferGraphics=img.getGraphics();
//Graphics bufferGraphics=img.getGraphics();
//Graphics bufferGraphics=canvas.getGraphics();
bufferGraphics.setColor(Color.cyan);
int smX=(CenterX%100);
int smY=(CenterY%100);
for (int i=0;i<Math.round(VisibleWidth/100)+2;i++){
int ix=i*100-smX;
bufferGraphics.drawLine(ix,0,ix,VisibleHeight);
}
for (int i=0;i<Math.round(VisibleHeight/100)+2;i++){
int iy=i*100-smY;
bufferGraphics.drawLine(0,iy,VisibleWidth,iy);
}
for (GameObject obj:Objects){
int objX=obj.GetX();
int objY=obj.GetY();
if (objX>CenterX-VisibleWidth/2 && objX<CenterX+VisibleWidth/2 && objY>CenterY-VisibleHeight/2 && objY<CenterY+VisibleHeight/2){
Image objImg=obj.draw();
bufferGraphics.drawImage(objImg,objX-obj.GetWidth()/2-(CenterX-VisibleWidth/2),objY-obj.GetHeight()/2-(CenterY-VisibleHeight/2),null);
}
}
bufferGraphics.setColor(Color.gray);
bufferGraphics.drawString("x:" + CenterX, 0, 10);
bufferGraphics.drawString("y:"+CenterY,0,20);
canvas.getGraphics().drawImage(img, 0, 0, null);
}
public void Tick(){
Objects.removeAll(ObjToRem);
Objects.addAll(ObjToAdd);
ObjToRem.clear();
ObjToAdd.clear();
for (GameObject obj:Objects){
obj.Tick(this);
}
}
public ArrayList<GameObject> getObject(int x,int y,int rad){
ArrayList<GameObject> result=new ArrayList<GameObject>();
for (GameObject obj:Objects){
int sx=obj.GetX();
int sy=obj.GetY();
int sr=obj.GetRadius();
int rr=(sx-x)*(sx-x)+(sy-y)*(sy-y)-(sr+rad)*(sr+rad);
if (rr<0){
result.add(obj);
}
}
return result;
}
}