Skip to content

Commit 1dc313f

Browse files
authored
Merge pull request #66 from Java-Game-Maker/paralax
Paralax
2 parents 82fd9c4 + 39ebf6c commit 1dc313f

File tree

9 files changed

+63
-18
lines changed

9 files changed

+63
-18
lines changed

JavaGameEngineDocumentation

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
Subproject commit d6014e015c5c34e2ea69fadc9cc09f729efd5628
1+
Subproject commit 6233d677e3b182548ab8527fc38e5a6cd3c85480

filename

6 KB
Binary file not shown.

src/com/javagamemaker/javagameengine/Scene.java

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -188,7 +188,7 @@ public void destroy(Component c) {
188188
}
189189

190190
public boolean inside(Component component) {
191-
return screen.contains(component.getShape().getBounds());
191+
return true;//screen.contains(component.getShape().getBounds()) || component.getShape().contains(screen);
192192
}
193193
public static void playSound(String path){
194194
playSound(path,1);
@@ -285,12 +285,26 @@ public int compare(Component o1, Component o2){
285285
});
286286

287287
try{
288+
//center graphics
289+
if(camera.parallax) graphics2D.translate(JavaGameEngine.getWindowSize().getX()/2, JavaGameEngine.getWindowSize().getY()/2);
290+
288291
int lsize = components.size();
289292
for(int i = 0; i < lsize;i++){
290293
Component c = renderList.get(i);
291294
if(inside(c) || true) {
292-
int layer = c.getLayer()==0?1:c.getLayer();
295+
//int layer = c.getLayer()==0?1:c.getLayer();
296+
float layer = c.getPosition().getZ()==0?1:c.getPosition().getZ();
297+
Camera camera = JavaGameEngine.getSelectedScene().getCamera();
298+
if(camera.parallax){
299+
Debug.log(camera.getPosition().getY());
300+
float parx = camera.getPosition().getX()*layer/100;
301+
float pary = camera.getPosition().getY()*layer/100;
302+
graphics2D.translate(parx,pary);
303+
}
293304
(c).render(graphics2D);
305+
if(camera.parallax){
306+
graphics2D.translate(-camera.getPosition().getX()*layer/100,-camera.getPosition().getY()*layer/100);
307+
}
294308
//if(!c.isVisible()){
295309
// c.setVisible(true);
296310
// c.onCameraEnter();

src/com/javagamemaker/javagameengine/components/Camera.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@
1212
* and its position controls the graphics position
1313
*/
1414
public class Camera extends Component {
15+
16+
public boolean parallax = false;
1517
@Override
1618
public void start() {
1719
super.start();

src/com/javagamemaker/javagameengine/components/Collider.java

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,12 +70,20 @@ public Point collision(Collider c){
7070
for (Vector2 vertex : vertices){
7171
Point p = new Point((int) vertex.getX(), (int) vertex.getY());
7272
if(c.getShape().contains(p)){
73+
try{
74+
//parent.setPosition(parent.getPosition().subtract(new Vector2(0,1)));
75+
}
76+
catch (Exception e){}
7377
return p;
7478
}
7579
}
7680
for (Vector2 vertex : c.vertices){
7781
Point p = new Point((int) vertex.getX(), (int) vertex.getY());
7882
if(getShape().contains(p)){
83+
try{
84+
//parent.setPosition(parent.getPosition().subtract(new Vector2(0,1)));
85+
}
86+
catch (Exception e){}
7987
return p;
8088
}
8189
}

src/com/javagamemaker/javagameengine/components/Component.java

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222
*/
2323
public class Component {
2424

25-
protected int layer = 100;
25+
protected int layer = 0;
2626
protected String tag = "";
2727
protected float angle = 0;
2828
protected boolean visible = true;
@@ -196,18 +196,20 @@ public void translate(Vector2 towards){
196196
if(collider!=null){
197197
Collider addedX = new Collider();
198198
addedX.localVertices = collider.getLocalVertices();
199-
addedX.setPosition(getPosition().add(towards.removeY()));
199+
addedX.setPosition(collider.getPosition().add(towards.removeY()));
200200
addedX.setScale(addedX.getScale().subtract(1));
201201
addedX.updateVertices();
202202

203203
Collider addedY = new Collider();
204204
addedY.localVertices = collider.getLocalVertices();
205-
addedY.setPosition(getPosition().add(towards.removeX()));
205+
addedY.setPosition(collider.getPosition().add(towards.removeX()));
206206
addedY.setScale(addedY.getScale().subtract(1));
207207
addedY.updateVertices();
208208

209209
// all components in the scene
210210
for ( Component c : JavaGameEngine.getSelectedScene().getComponents1() ){
211+
if(c.getPosition().getZ() != getPosition().getZ()) continue;
212+
211213
if(c != this && JavaGameEngine.getSelectedScene().inside(c)){ // don't check us
212214
for ( Component cc : c.getChildren(new Collider()) ){
213215
Collider otherCollider = (Collider) cc;
@@ -242,9 +244,9 @@ else if(otherCollider.isTrigger()){
242244
try{
243245
Vector2 vel = ((PhysicsBody) getChild(new PhysicsBody())).velocity;
244246
((PhysicsBody) getChild(new PhysicsBody())).response(event);
245-
if(((PhysicsBody) getChild(new PhysicsBody())).velocity.getX() == vel.getX()){
247+
if(this.<PhysicsBody>getChild().velocity.getX() == vel.getX()){
246248
//Debug.log("zeor");
247-
((PhysicsBody) getChild(new PhysicsBody())).velocity.setX(0);
249+
this.<PhysicsBody>getChild().velocity.setX(0);
248250
}
249251
}catch (Exception e){}
250252
}
@@ -294,7 +296,7 @@ else if(otherCollider.isTrigger()){
294296
public void setPosition(Vector2 position) {
295297
//this.lastPosition = this.position;
296298

297-
if(getParent()!=null){
299+
if(getParent() != null){
298300
this.position = position.add(parentOffset).add(rotOffset);
299301

300302
}else{
@@ -463,13 +465,14 @@ public LinkedList<Component> getAllChildren(Component type){
463465
}
464466

465467
public <T>T getChild(){
468+
T t = null;
466469
for (Component child : this.children){
467470
try{
468-
return ((T)child);
471+
t = ((T)child);
469472
}
470473
catch (Exception e){}
471474
}
472-
return null;
475+
return t;
473476
}
474477
/**
475478
* @param type the specified type of the children to be returned

src/com/javagamemaker/javagameengine/components/GameObject.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,14 +38,14 @@ public void setColor(Color color) {
3838
public void render(Graphics2D g) {
3939
if(visible){
4040
Camera camera = JavaGameEngine.getSelectedScene().getCamera();
41-
//g.translate(camera.getPosition().getX()*layer/100,camera.getPosition().getY()*layer/100);
4241
Color prev = g.getColor();
4342
if(getChild(new Sprite())==null){
4443
g.setColor(color);
4544
g.fill(getShape());
4645
g.setColor(prev);
4746
}
4847
renderChildren(g);
48+
//g.translate(camera.getPosition().getX()*layer/100,camera.getPosition().getY()*layer/100);
4949
//g.translate(-camera.getPosition().getX()*layer/100,-camera.getPosition().getY()*layer/100);
5050
}
5151
}

src/com/javagamemaker/javagameengine/components/Grabber.java

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,10 @@ public Grabber(Component parent){
1717
public void update() {
1818
super.update();
1919
if (Input.isMouseDown(1) && parent.isMouseInside()) {
20-
Debug.log("asd");
20+
PhysicsBody b;
21+
if ((b = ((PhysicsBody)parent.getChild(new PhysicsBody()))) != null){
22+
b.velocity = Vector2.zero;
23+
}
2124
if (this.offset == null) {
2225
this.offset = parent.getPosition().subtract(Input.getMousePosition());
2326
}

src/com/javagamemaker/javagameengine/msc/Vector2.java

Lines changed: 20 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,15 +9,22 @@ public class Vector2 {
99
public static Vector2 right = new Vector2(1,0);
1010
public static Vector2 left = new Vector2(-1,0);
1111
public static Vector2 zero = new Vector2(0,0);
12-
private float x,y;
12+
private float x,y,z;
1313

14+
public Vector2(float x, float y, float z) {
15+
this.x = x;
16+
this.y = y;
17+
this.z = z;
18+
}
1419
public Vector2(float x, float y) {
1520
this.x = x;
1621
this.y = y;
22+
this.z = 0;
1723
}
1824
public Vector2(Vector2 vector2) {
1925
this.x = vector2.x;
2026
this.y = vector2.y;
27+
this.z = vector2.z;
2128
}
2229
public float getX() {
2330
return x;
@@ -35,6 +42,14 @@ public void setY(float y) {
3542
this.y = y;
3643
}
3744

45+
public float getZ() {
46+
return z;
47+
}
48+
49+
public void setZ(float z) {
50+
this.z = z;
51+
}
52+
3853
public String toString()
3954
{
4055
return ("{x:{"+x+"} y:{"+y+"}}");
@@ -44,10 +59,10 @@ public Vector2 multiply(float multiple) {
4459
return new Vector2(x*multiple,y*multiple);
4560
}
4661
public Vector2 multiply(double multiple) {
47-
return new Vector2((float) (x*multiple), (float) (y*multiple));
62+
return new Vector2((float) (x*multiple), (float) (y*multiple),(float) (z*multiple));
4863
}
4964
public Vector2 multiply(Vector2 vector2) {
50-
return new Vector2(x*vector2.x,y*vector2.y);
65+
return new Vector2(x*vector2.x,y*vector2.y,z*vector2.z);
5166
}
5267

5368
public float getMagnitude (){
@@ -63,10 +78,10 @@ public Vector2 getOpposite(){
6378
}
6479

6580
public Vector2 divide(float a){
66-
return new Vector2(x/a,y/a);
81+
return new Vector2(x/a,y/a, z/a);
6782
}
6883
public Vector2 divide(Vector2 a){
69-
return new Vector2(x/a.getX(),y/a.getY());
84+
return new Vector2(x/a.getX(),y/a.getY(), z/a.getZ());
7085
}
7186
public Vector2 add(Vector2 vector2) {
7287
return new Vector2(x+ vector2.x,y+ vector2.y);

0 commit comments

Comments
 (0)