Hello
I`ve been coding for about 20 hours ( xD ) and my brain does not work. I tried various combinations in detecting collisions... and none works. Below all code:
/**
* @(#)GameFromScratch.java
*
* GameFromScratch application
*
* @author #cx3#
* @version 1.00 2015/3/7
*/ import org.jsfml.graphics.RenderWindow;import org.jsfml.graphics.Text;import org.jsfml.graphics.*;import org.jsfml.system.Clock;import org.jsfml.system.Time;import org.jsfml.window.VideoMode;import org.jsfml.window.WindowStyle;import org.jsfml.window.event.Event; import org.jsfml.graphics.Color; import org.jsfml.graphics.Font;import org.jsfml.graphics.Texture;import org.jsfml.graphics.Sprite;import org.jsfml.system.*;import org.jsfml.system.Time;import org.jsfml.system.Vector2f;import org.jsfml.window.Keyboard;import org.jsfml.window.event.Event;import java.util.HashMap;import java.util.Map;import java.util.ArrayList;import java.io.*;import java.io.FileInputStream;import java.io.File;import java.io.IOException;import java.util.HashMap;import java.nio.file.Paths;import java.nio.file.*;import java.lang.Number;import java.lang.Math;import java.io.FileNotFoundException;import java.util.Scanner;import java.util.Arrays;import java.lang.System;/*
* @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
*/enum KeybAction
{ left,right,up,down,none,esc
};enum SpriteType
{ PLAYER, ENEMY, FRUIT, BRICK, BACK
};class INFO
{ public static String MEDIA_DIR
; public static Bricks BRICKS
; public static RenderWindow GAMEWINDOW
;}//################################################//################################################interface IGetSprites
{ public Sprite
[] getSprites
(); }interface IDrawRenderWindow
{ public void draw
(RenderWindow rw
); }//################################################ class ImageLoader
{ Texture texture
= new Texture
(); Sprite sprite
= new Sprite
(); Image image
= new Image(); SpriteType spriteType
; int x,y
; public ImageLoader
(String spritePath, SpriteType spriteType
) { x
=0; y
=0; this.
spriteType = spriteType
; try { texture.
loadFromFile(Paths.
get(spritePath
)); texture.
setRepeated(true); sprite
= new Sprite
(texture
); image
= texture.
copyToImage(); } catch (Exception e
) { e.
printStackTrace(); } } public ImageLoader
(String spritePath, SpriteType spriteType,
int x,
int y
) { this(spritePath, spriteType
); this.
spriteType = spriteType
; this.
x = x
; this.
y = y
; } public Sprite copySprite
() { Texture tx
= new Texture
(texture
); tx.
setRepeated(true); return new Sprite
(sprite.
getTexture()); } public Sprite copySprite
(int x,
int y
) { Sprite s
= copySprite
(); s.
setPosition(x,y
); return s
; } public void setX
(int x
){ this.
x = x
; } public void setY
(int y
){ this.
y = y
; } public void setXY
(int x,
int y
){ setX
(x
); setY
(y
); } public Sprite getSprite
() { sprite.
setPosition(x,y
); return sprite
; } public Sprite getSprite
(int x,
int y
) { sprite.
setPosition(x,y
); return sprite
; } public Image getImage
(){ return image
; } public Texture getTexture
(){ return texture
; } public SpriteType getSpriteType
(){ return spriteType
; }} //################################################//################################################class SafeZone
{ private static boolean isColliding
//arglist ( Vector2f Ax1y1,
Vector2f Ax2y2,
Vector2f Bx1y1,
Vector2f Bx2y2
) {//method`s body start: float widthA
= Math.
abs(Ax1y1.
x - Ax2y2.
x),
widthB
= Math.
abs(Bx1y1.
x - Bx2y2.
x),
heightA
= Math.
abs(Ax1y1.
y - Ax2y2.
y),
heightB
= Math.
abs(Bx1y1.
y - Bx2y2.
y); if ( (Math.
abs(Ax1y1.
x - Bx2y2.
x) <= widthA
+ widthB
) && Math.
abs(Ax1y1.
y - Bx2y2.
y) <= heightA
+ heightB
) return true; return false; }public static boolean isColliding
(Sprite A, Sprite B
) { Vector2f
vA1
= A.
getPosition(), vA2
= A.
getOrigin(),
vB1
= B.
getPosition(), vB2
= B.
getOrigin(); float vAdtx
= vA2.
x - vA1.
x,
vAdty
= vA2.
y - vA1.
y,
vBdtx
= vB2.
x - vB1.
x,
vBdty
= vB2.
y - vB1.
y; vA2
= new Vector2f
(vA1.
x, vA1.
x + 2*vAdtx
); vB2
= new Vector2f
(vB1.
x, vB1.
x + 2*vBdtx
); return isColliding
(vA1, vA2, vB1, vB2
); } public static boolean isColliding
(Sprite A, Sprite
[] sprites
) { for (Sprite sprite
: sprites
) if (isColliding
( sprite, A
) == true ) return true; return false; }}//################################################//################################################ class Bricks
implements IGetSprites, IDrawRenderWindow
{ ArrayList
<Sprite
> bricks
; //ArrayList<FloatRect> rectangles; ImageLoader image
; Texture texture
; public Bricks
(ImageLoader image,
String mapFile
) { this.
image = image
; bricks
= new ArrayList
<Sprite
>(); loadMapFile
(mapFile
); } private void loadMapFile
(String map
) { Vector2i v
= image.
getTexture().
getSize(); try { Scanner scanner
= new Scanner
( new File(map
) ); for (int x
=0; x
<7; ++x
) { String line
= scanner.
nextLine(); for (int y
=0; y
<7; ++y
) if (line.
charAt(y
)!='.') bricks.
add( image.
copySprite( (1+x
)*v.
x,
(1+y
)*v.
y) ); } //rectangles = new ArrayList<FloatRect>(); } catch(Exception e
) { e.
printStackTrace(); } } @Override
public Sprite
[] getSprites
() { int s
= bricks.
size(); Sprite
[] ret
= new Sprite
[s
]; for (int i
=0; i
<s
; ++i
) ret
[i
] = bricks.
get(i
); return ret
; } @Override
public void draw
(RenderWindow rw
) { for (Sprite sprite
: bricks
) rw.
draw(sprite
); }} //################################################//################################################class FramedAnimation
implements IGetSprites, IDrawRenderWindow
{ ArrayList
<ImageLoader
> frames
; Clock clock
; int x, y, current, timePerFrame
; public FramedAnimation
(String []imgFiles, SpriteType spriteType
) { frames
= new ArrayList
<ImageLoader
>(); current
= 0; for (int i
=0; i
<imgFiles.
length; ++i
) frames.
add(new ImageLoader
(imgFiles
[i
],spriteType
) ); setTimePerFrame
(1000); } public FramedAnimation
(String []imgFiles, SpriteType spriteType,
int x,
int y
) { this(imgFiles, spriteType
); this.
setXY(x,y
); } public FramedAnimation
(ArrayList
<ImageLoader
> frames
) { this.
frames = frames
; current
= 0; setTimePerFrame
(1000); } public void setTimePerFrame
(int msTimePerFrame
) { if (clock
== null) clock
= new Clock
(); timePerFrame
= msTimePerFrame
; clock.
restart(); } public void setXY
(int x,
int y
) { for (ImageLoader image
: frames
) image.
setXY(x,y
); } private void setFrame
() { if (clock.
getElapsedTime().
asMilliseconds() >= timePerFrame
) { current
++; clock.
restart(); } if ( current
>= frames.
size() ) current
= 0; } @Override
//IGetSprites public Sprite
[] getSprites
() { setFrame
(); Sprite sprite
= frames.
get(current
).
getSprite(); sprite.
setPosition(x,y
); return new Sprite
[]{ sprite
}; } @Override
//IDrawRenderWindow public void draw
(RenderWindow rw
) { for (Sprite sprite
: getSprites
()) rw.
draw(sprite
); }}//################################################//################################################class Background
implements IGetSprites, IDrawRenderWindow
{ FramedAnimation frames
; Vector2i vsize
; Time lastUpdate
; public Background
(FramedAnimation animation,
int msPerFrame
) { frames
= animation
; frames.
setTimePerFrame(msPerFrame
); } @Override
//IGetSprites public Sprite
[] getSprites
() { Sprite
[]sprites
= frames.
getSprites(); vsize
= sprites
[0].
getTexture().
getSize(); ArrayList
<Sprite
> background
= new ArrayList
<Sprite
>(); int index
=0; for (int x
=0; x
<800+vsize.
x; x
+=vsize.
x) for (int y
=0; y
<600+vsize.
y; y
+=vsize.
y) { if (index
== sprites.
length-1) index
=0; else index
+=1; Sprite sp
= new Sprite
( sprites
[index
].
getTexture() ); sp.
setPosition(x,y
); background.
add( sp
); } return background.
toArray( new Sprite
[ background.
size() ] ); } @Override
//IDrawRenderWindow public void draw
(RenderWindow rw
) { for ( Sprite sprite
: this.
getSprites() ) rw.
draw(sprite
); }}//################################################//################################################class Player
implements IGetSprites
{ ImageLoader image
; Vector2i last, vleft, vright, vup, vdown
; Sprite
[] bricks
; int speed
;public Player
(ImageLoader loader,
int startx,
int starty,
int speed
) { image
= loader
; image.
setXY(100,
100); last
= new Vector2i
(100,
100); this.
speed = speed
; } public void setBricksMap
(Sprite
[] safeZone
) { this.
bricks = /*new ArrayList<Sprite>( */ safeZone
; /* );*/ } /*
* this code should checks collisions!
*/ public void tryMoveTo
(Vector2f newPosition
) { FloatRect pos
= image.
getSprite().
getLocalBounds(); Vector2i vi
= new Vector2i
(newPosition
); if (vi.
x> 800 || vi.
x<0 || vi.
y>600-pos.
top || vi.
y<pos.
height) return; if (SafeZone.
isColliding(image.
getSprite(), bricks
) == true) { System.
out.
println("KOLIZJA"); return; } image.
setXY(vi.
x, vi.
y); last
= vi
; } public void nextMove
(Time dt, KeybAction action
) { if (action
== KeybAction.
none) return; float fmove
= (dt.
asMicroseconds() * speed
)/5000; switch (action
) { case left
: tryMoveTo
(new Vector2f
(last.
x -fmove, last.
y)); break; case right
: tryMoveTo
(new Vector2f
(last.
x +fmove, last.
y)); break; case up
: tryMoveTo
(new Vector2f
(last.
x, last.
y -fmove
)); break; case down
: tryMoveTo
(new Vector2f
(last.
x, last.
y +fmove
)); break; } } @Override
// IGetSprites public Sprite
[] getSprites
() { return new Sprite
[]{ image.
getSprite() }; }} /** ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
GAME`S MAIN LOOP
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ **/class GameLoop
{ //internal helper class class World
{ ArrayList
<Sprite
> list
;// list of sprites to be drawn RenderWindow window
; public World
(RenderWindow renderWindow
) { list
= new ArrayList
<Sprite
>(); this.
window = renderWindow
; } public void add
(IGetSprites toBeDrawn
) //adds by interface { if (toBeDrawn
==null) return; Sprite
[]sprites
= toBeDrawn.
getSprites(); if (sprites
==null || sprites.
length == 0) return; this.
add(sprites
); } public void add
(Sprite
[] sprites
) //adds by array { for (Sprite s
: sprites
) list.
add(s
); } public void clear
() { list.
clear(); } public void render
(RenderWindow rw
) { rw.
clear(); for (Sprite s
: list
) rw.
draw(s
); rw.
display(); this.
clear(); } public void render
() { render
(window
); } public void close
() { this.
clear(); //window.clear(); window.
close(); } } /* - - - -
GameLoop`s interface
- - - - */ FloatRect bounds
; World world
; Background background
; Bricks bricks
; Player player
; Time timePerFrame
; RenderWindow window
; String mediaDirPath
; boolean isRun
; /* ~ ~ ~
IMPLEMENTATION
~ ~ ~ */ public GameLoop
(String mediaDirPath
) { this.
mediaDirPath = mediaDirPath
; timePerFrame
= Time.
getSeconds(1.0f
/ 60.0f
); bounds
= new FloatRect
(new IntRect
(0,
0,
800,
600)); window
= new RenderWindow
(new VideoMode
(800,
600),
"GameFromScratch"); background
= new Background
( new FramedAnimation
( new ArrayList
<ImageLoader
> ( Arrays.
asList ( new ImageLoader
[] { new ImageLoader
("C:\\PROJ\\Media\\Maps\\map1.png",SpriteType.
BACK),
new ImageLoader
("C:\\PROJ\\Media\\Maps\\map2.png",SpriteType.
BACK),
new ImageLoader
("C:\\PROJ\\Media\\Maps\\map3.png",SpriteType.
BACK),
new ImageLoader
("C:\\PROJ\\Media\\Maps\\map4.png",SpriteType.
BACK),
} ) ) ),
250 //ms per frame ); bricks
= new Bricks
( new ImageLoader
( "c:\\PROJ\\MEDIA\\Maps\\map1.png",
SpriteType.
BRICK ),
"C:\\PROJ\\Media\\Maps\\map1.txt" ); player
= new Player
( new ImageLoader
( mediaDirPath
+"\\Textures\\player2.png",
SpriteType.
PLAYER ),
100,
//startX 100,
//startY 5 //speed ); player.
setBricksMap(bricks.
getSprites()); } private KeybAction getKey
() { for (Event event
: window.
pollEvents()) { if (Keyboard.
isKeyPressed(Keyboard.
Key.
LEFT)) return KeybAction.
left; if (Keyboard.
isKeyPressed(Keyboard.
Key.
RIGHT)) return KeybAction.
right; if (Keyboard.
isKeyPressed(Keyboard.
Key.
UP)) return KeybAction.
up; if (Keyboard.
isKeyPressed(Keyboard.
Key.
DOWN)) return KeybAction.
down; if (event.
type == Event.
Type.
CLOSED) return KeybAction.
esc; if (Keyboard.
isKeyPressed(Keyboard.
Key.
ESCAPE)) return KeybAction.
esc; } return KeybAction.
none; } public void playGame
() { Clock clock
= new Clock
(); Time lastUpdate
= Time.
ZERO; window
= new RenderWindow
(new VideoMode
(800,
600),
"JSFML RenderTest"); world
= new World
(window
); isRun
= true; window.
setFramerateLimit(60); window.
setKeyRepeatEnabled(true); KeybAction key
= KeybAction.
none; while( window.
isOpen() ) { Time dt
= clock.
restart(); lastUpdate
= Time.
add(lastUpdate, dt
); while (lastUpdate.
asMicroseconds() > timePerFrame.
asMicroseconds()) { lastUpdate
= Time.
sub(lastUpdate, timePerFrame
); key
= getKey
(); player.
nextMove(lastUpdate, key
); if (key
==KeybAction.
esc) window.
close(); } world.
add(background
); world.
add(player
); world.
add(bricks
); world.
render(); } world.
close(); }} /**
* Run
*/ public class GameFromScratch
{ public static void main
(String[] args
) { INFO.
MEDIA_DIR = "C:\\PROJ\\Media"; System.
out.
println("tak"); GameLoop game
= new GameLoop
(INFO.
MEDIA_DIR); game.
playGame(); }} Can anybody help? In attachments media for game.
I know that code should be separatet etc, I`ll tidy it in near future. I'm planning to create tutorial about JSFML, it's f.. amazing lib!