diff --git a/.classpath b/.classpath index 865fa06..f831748 100644 --- a/.classpath +++ b/.classpath @@ -6,5 +6,6 @@ + diff --git a/bin/John.png b/bin/John.png new file mode 100644 index 0000000..4a84953 Binary files /dev/null and b/bin/John.png differ diff --git a/bin/Map.png b/bin/Map.png new file mode 100644 index 0000000..d7267e4 Binary files /dev/null and b/bin/Map.png differ diff --git a/bin/Rabbi.png b/bin/Rabbi.png new file mode 100644 index 0000000..8e1f6a6 Binary files /dev/null and b/bin/Rabbi.png differ diff --git a/bin/RabbiLeft.png b/bin/RabbiLeft.png new file mode 100644 index 0000000..3337d82 Binary files /dev/null and b/bin/RabbiLeft.png differ diff --git a/bin/application/Main$1.class b/bin/application/Main$1.class new file mode 100644 index 0000000..791d718 Binary files /dev/null and b/bin/application/Main$1.class differ diff --git a/bin/application/Main.class b/bin/application/Main.class index be898d9..4c5aff3 100644 Binary files a/bin/application/Main.class and b/bin/application/Main.class differ diff --git a/bin/drawing/GameScreen.class b/bin/drawing/GameScreen.class new file mode 100644 index 0000000..457a281 Binary files /dev/null and b/bin/drawing/GameScreen.class differ diff --git a/bin/input/InputUtility.class b/bin/input/InputUtility.class new file mode 100644 index 0000000..8bb7e1e Binary files /dev/null and b/bin/input/InputUtility.class differ diff --git a/bin/logic/charClass/BaseCharClass.class b/bin/logic/charClass/BaseCharClass.class deleted file mode 100644 index fc5d069..0000000 Binary files a/bin/logic/charClass/BaseCharClass.class and /dev/null differ diff --git a/bin/logic/entity/BaseEntity.class b/bin/logic/entity/BaseEntity.class deleted file mode 100644 index ddec0c3..0000000 Binary files a/bin/logic/entity/BaseEntity.class and /dev/null differ diff --git a/bin/logic/entity/Entity.class b/bin/logic/entity/Entity.class new file mode 100644 index 0000000..d2c5042 Binary files /dev/null and b/bin/logic/entity/Entity.class differ diff --git a/bin/logic/entity/Player.class b/bin/logic/entity/Player.class new file mode 100644 index 0000000..723a48c Binary files /dev/null and b/bin/logic/entity/Player.class differ diff --git a/bin/logic/entity/Werewolf.class b/bin/logic/entity/Werewolf.class new file mode 100644 index 0000000..9023c4c Binary files /dev/null and b/bin/logic/entity/Werewolf.class differ diff --git a/bin/logic/field/Map1.class b/bin/logic/field/Map1.class new file mode 100644 index 0000000..8311ced Binary files /dev/null and b/bin/logic/field/Map1.class differ diff --git a/bin/logic/field/WhiteMap.class b/bin/logic/field/WhiteMap.class new file mode 100644 index 0000000..6ac3341 Binary files /dev/null and b/bin/logic/field/WhiteMap.class differ diff --git a/bin/logic/game/GameLogic.class b/bin/logic/game/GameLogic.class new file mode 100644 index 0000000..fe3809c Binary files /dev/null and b/bin/logic/game/GameLogic.class differ diff --git a/bin/sharedObject/IRenderable.class b/bin/sharedObject/IRenderable.class new file mode 100644 index 0000000..b81aba6 Binary files /dev/null and b/bin/sharedObject/IRenderable.class differ diff --git a/bin/sharedObject/RenderableHolder.class b/bin/sharedObject/RenderableHolder.class new file mode 100644 index 0000000..4c866df Binary files /dev/null and b/bin/sharedObject/RenderableHolder.class differ diff --git a/res/John.png b/res/John.png new file mode 100644 index 0000000..4a84953 Binary files /dev/null and b/res/John.png differ diff --git a/res/Map.png b/res/Map.png new file mode 100644 index 0000000..d7267e4 Binary files /dev/null and b/res/Map.png differ diff --git a/res/Rabbi.png b/res/Rabbi.png new file mode 100644 index 0000000..8e1f6a6 Binary files /dev/null and b/res/Rabbi.png differ diff --git a/res/RabbiLeft.png b/res/RabbiLeft.png new file mode 100644 index 0000000..3337d82 Binary files /dev/null and b/res/RabbiLeft.png differ diff --git a/src/application/Main.java b/src/application/Main.java index 66388cd..12bf25f 100644 --- a/src/application/Main.java +++ b/src/application/Main.java @@ -1,10 +1,62 @@ package application; -public class Main { +import drawing.GameScreen; +import input.InputUtility; +import javafx.animation.AnimationTimer; +import javafx.application.Application; +import javafx.scene.Scene; +import javafx.scene.layout.StackPane; +import javafx.stage.Stage; +import logic.game.GameLogic; +import logic.entity.Player; +import logic.entity.Werewolf; +import sharedObject.RenderableHolder; + +public class Main extends Application{ public static void main(String[] args) { // TODO Auto-generated method stub + Application.launch(args); + } + + @Override + public void start(Stage stage) throws Exception { + final int width = 1280; + final int height = 720; + // TODO Auto-generated method stub + StackPane root = new StackPane(); + Scene scene = new Scene(root); + stage.setScene(scene); + stage.setTitle("2D Game"); + + + GameScreen gameScreen = new GameScreen(width, height); + GameLogic logic = new GameLogic(gameScreen); + root.getChildren().add(gameScreen); + gameScreen.requestFocus(); + + stage.show(); + + AnimationTimer animation = new AnimationTimer() { + long previousTime = 0; + double drawInterval = 1e9/30; + double delta = 0; + + public void handle(long now) { + delta += (now-previousTime)/drawInterval; + previousTime = now; + + if (delta >= 1) { + gameScreen.paintComponent(); + logic.logicUpdate(); + RenderableHolder.getInstance().update(); + InputUtility.updateInputState(); + delta--; + } + } + }; + animation.start(); } } diff --git a/src/drawing/GameScreen.java b/src/drawing/GameScreen.java new file mode 100644 index 0000000..3a93850 --- /dev/null +++ b/src/drawing/GameScreen.java @@ -0,0 +1,74 @@ +package drawing; + +import input.InputUtility; +import javafx.scene.canvas.Canvas; +import javafx.scene.canvas.GraphicsContext; +import javafx.scene.input.KeyEvent; +import javafx.scene.input.MouseButton; +import javafx.scene.input.MouseEvent; +import javafx.scene.paint.Color; +import sharedObject.IRenderable; +import sharedObject.RenderableHolder; + +public class GameScreen extends Canvas{ + public GameScreen(double width, double height) { + super(width, height); + this.setVisible(true); + this. + addListerner(); + } + + public void addListerner() { + this.setOnKeyPressed((KeyEvent event) -> { + InputUtility.setKeyPressed(event.getCode(), true); + }); + + this.setOnKeyReleased((KeyEvent event) -> { + InputUtility.setKeyPressed(event.getCode(), false); + }); + + this.setOnMousePressed((MouseEvent event) -> { + if (event.getButton() == MouseButton.PRIMARY) + InputUtility.mouseLeftDown(); + }); + + this.setOnMouseReleased((MouseEvent event) -> { + if (event.getButton() == MouseButton.PRIMARY) + InputUtility.mouseLeftRelease(); + }); + + this.setOnMouseEntered((MouseEvent event) -> { + InputUtility.mouseOnScreen = true; + }); + + this.setOnMouseExited((MouseEvent event) -> { + InputUtility.mouseOnScreen = false; + }); + + this.setOnMouseMoved((MouseEvent event) -> { + if (InputUtility.mouseOnScreen) { + InputUtility.mouseX = event.getX(); + InputUtility.mouseY = event.getY(); + } + }); + + this.setOnMouseDragged((MouseEvent event) -> { + if (InputUtility.mouseOnScreen) { + InputUtility.mouseX = event.getX(); + InputUtility.mouseY = event.getY(); + } + }); + } + + public void paintComponent() { + GraphicsContext gc = this.getGraphicsContext2D(); + gc.setFill(Color.BLACK); + gc.fillRect(0, 0, getWidth(), getHeight()); + for (IRenderable entity : RenderableHolder.getInstance().getEntities()) { + // System.out.println(entity.getZ()); + if (entity.isVisible() && !entity.isDestroyed()) { + entity.draw(gc); + } + } + } +} diff --git a/src/input/InputUtility.java b/src/input/InputUtility.java new file mode 100644 index 0000000..9030143 --- /dev/null +++ b/src/input/InputUtility.java @@ -0,0 +1,45 @@ +package input; + +import java.util.ArrayList; + +import javafx.scene.input.KeyCode; + +public class InputUtility { + public static double mouseX,mouseY; + public static boolean mouseOnScreen = true; + private static boolean isLeftDown = false; + private static boolean isLeftClickedLastTick = false; + private static ArrayList keyPressed = new ArrayList<>(); + + public static boolean getKeyPressed(KeyCode keycode) { + return keyPressed.contains(keycode); + } + public static void setKeyPressed(KeyCode keycode,boolean pressed) { + if(pressed){ + if(!keyPressed.contains(keycode)){ + keyPressed.add(keycode); + } + }else{ + keyPressed.remove(keycode); + } + System.out.println(keyPressed); + } + + public static void mouseLeftDown(){ + isLeftDown = true; + isLeftClickedLastTick = true; + } + + public static void mouseLeftRelease(){ + isLeftDown = false; + } + + public static boolean isLeftClickTriggered(){ + return isLeftClickedLastTick; + } + + public static void updateInputState(){ + isLeftClickedLastTick = false; + } + +} diff --git a/src/logic/charClass/BaseCharClass.java b/src/logic/charClass/BaseCharClass.java deleted file mode 100644 index 1501668..0000000 --- a/src/logic/charClass/BaseCharClass.java +++ /dev/null @@ -1,5 +0,0 @@ -package logic.charClass; - -public class BaseCharClass { - -} diff --git a/src/logic/entity/BaseEntity.java b/src/logic/entity/BaseEntity.java deleted file mode 100644 index 70758f7..0000000 --- a/src/logic/entity/BaseEntity.java +++ /dev/null @@ -1,5 +0,0 @@ -package logic.entity; - -public class BaseEntity { - -} diff --git a/src/logic/entity/Entity.java b/src/logic/entity/Entity.java new file mode 100644 index 0000000..5186f21 --- /dev/null +++ b/src/logic/entity/Entity.java @@ -0,0 +1,49 @@ +package logic.entity; + +import sharedObject.IRenderable; + +public abstract class Entity implements IRenderable{ + protected double worldX,worldY; + public double screenX,screenY; + protected int z,radius; + protected boolean visible,destroyed; + protected int speed; + protected String direction; + + protected Entity(){ + visible = true; + destroyed = false; + } + + public abstract void attack(); + + public boolean canAttack(double x1,double y1,double x2,double y2,int attackRange) { + return (Math.abs(x1-x2) < attackRange && Math.abs(y1-y2) < attackRange); + } + + @Override + public int getZ() { + // TODO Auto-generated method stub + return z; + } + + @Override + public boolean isDestroyed() { + // TODO Auto-generated method stub + return destroyed; + } + + @Override + public boolean isVisible() { + // TODO Auto-generated method stub + return visible; + } + + public double getWorldX() { + return worldX; + } + + public double getWorldY() { + return worldY; + } +} diff --git a/src/logic/entity/Player.java b/src/logic/entity/Player.java new file mode 100644 index 0000000..1ad1206 --- /dev/null +++ b/src/logic/entity/Player.java @@ -0,0 +1,67 @@ +package logic.entity; + + + +import input.InputUtility; +import javafx.scene.canvas.GraphicsContext; +import javafx.scene.input.KeyCode; +import javafx.scene.paint.Color; +import logic.game.GameLogic; +import sharedObject.RenderableHolder; + +public class Player extends Entity{ + + public GameLogic gameLogic; + public Player(int x, int y,GameLogic gameLogic) { + this.worldX = x; + this.worldY = y; + this.speed = 3; + this.radius = 32; + this.gameLogic = gameLogic; + this.direction = "right"; + screenX = gameLogic.getGameScreen().getWidth()/2-radius; + screenY = gameLogic.getGameScreen().getHeight()/2-radius; + + } + @Override + public void draw(GraphicsContext gc) { + // TODO Auto-generated method stub + switch(direction) { + case "left": + gc.drawImage(RenderableHolder.playerLeft, screenX, screenY); + break; + case "right": + gc.drawImage(RenderableHolder.playerRight, screenX, screenY); + break; + } +// gc.setFill(Color.BLACK); +// gc.fillRect(gameLogic.getGameScreen().getWidth()/2, gameLogic.getGameScreen().getHeight()/2, 1, 1); + } + + public void attack() { + + } + + public void update() { + // TODO Auto-generated method stub + if (InputUtility.getKeyPressed(KeyCode.W)) { + worldY -= speed; + } + if (InputUtility.getKeyPressed(KeyCode.A)) { + worldX -= speed; + direction = "left"; + } + if (InputUtility.getKeyPressed(KeyCode.S)) { + worldY += speed; + } + if (InputUtility.getKeyPressed(KeyCode.D)) { + worldX += speed; + direction = "right"; + } + + if (InputUtility.isLeftClickTriggered()) { + + } + } + +} diff --git a/src/logic/entity/Werewolf.java b/src/logic/entity/Werewolf.java new file mode 100644 index 0000000..6cef8cf --- /dev/null +++ b/src/logic/entity/Werewolf.java @@ -0,0 +1,52 @@ +package logic.entity; + +import javafx.scene.canvas.GraphicsContext; +import javafx.scene.paint.Color; +import logic.game.GameLogic; +import sharedObject.RenderableHolder; + + +public class Werewolf extends Entity{ + private double angle = 0; + public GameLogic gameLogic; + public Werewolf(int x, int y,GameLogic gameLogic) { + this.worldX = x; + this.worldY = y; + this.z = -100; + this.speed =1; + this.gameLogic = gameLogic;; + } + @Override + public void draw(GraphicsContext gc) { + // TODO Auto-generated method stub + screenX = worldX-gameLogic.getPlayer().worldX+gameLogic.getPlayer().screenX; + screenY = worldY-gameLogic.getPlayer().worldY+gameLogic.getPlayer().screenY; + gc.drawImage(RenderableHolder.johnSprite, screenX, screenY); + } + + public void update(Player player) { + // TODO Auto-generated method stub + if (!canAttack(player.worldX, player.worldY, worldX, worldY, 24)) { + angle = Math.atan2(player.worldY-worldY,player.worldX-worldX); + double xspeed = Math.cos(angle) * speed; + double yspeed = Math.sin(angle) * speed; + + if (xspeed<0) + direction = "left"; + else direction = "right"; + + worldX += xspeed; + worldY += yspeed; + } + + else { + attack(); + } + } + @Override + public void attack() { + // TODO Auto-generated method stub + + } + +} diff --git a/src/logic/field/Map1.java b/src/logic/field/Map1.java new file mode 100644 index 0000000..b8d3970 --- /dev/null +++ b/src/logic/field/Map1.java @@ -0,0 +1,118 @@ +package logic.field; + +import javafx.scene.canvas.GraphicsContext; +import javafx.scene.image.WritableImage; +import javafx.scene.shape.Rectangle; +import logic.entity.Player; +import logic.game.GameLogic; +import sharedObject.IRenderable; +import sharedObject.RenderableHolder; + +public class Map1 implements IRenderable{ + public GameLogic gameLogic; + private WritableImage[] croppedImage = new WritableImage[4]; + + public Map1(GameLogic gameLogic) { + this.gameLogic = gameLogic; + croppedImage[0] = new WritableImage(RenderableHolder.mapSprite.getPixelReader(), + 0 * tileSize, 0, tileSize, tileSize); + croppedImage[1] = new WritableImage(RenderableHolder.mapSprite.getPixelReader(), + 1 * tileSize, 0, tileSize, tileSize); + croppedImage[2] =new WritableImage(RenderableHolder.mapSprite.getPixelReader(), + 2 * tileSize, 0, tileSize, tileSize); + } + int tileSize = 64; + private static int[][] field = { { -1, 0, 0, 0, 0, 0, 0, 0, -1,-1, 0, 0, 0, 0, 0, 0, 0, -1, 0 ,-1, 0, 0, 0, 0, 0, 0, 0, -1,0, 0, 0, 0, 0, 0, 0, 0, 0, 0}, + {-1, 0, 0, 0, 0, 0, 0, 0, -1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 ,-1, 0, 0, 0, 0, 0, 0, 0, -1,0, 0, 0, 0, 0, 0, 0, 0, 0, 0}, + { -1, 0, 0, 0, 0, 0, 0, 0, -1,0, 0, 0, 0, 0, 0, 0, 0, 0, 0 ,-1, 0, 0, 0, 0, 0, 0, 0, -1,0, 0, 0, 0, 0, 0, 0, 0, 0, 0}, + { -1, 0, 0, 0, 0, 0, 0, 0, -1,0, 0, 0, 0, 0, 0, 0, 0, 0, 0 ,-1, 0, 0, 0, 0, 0, 0, 0, -1,0, 0, 0, 0, 0, 0, 0, 0, 0, 0}, + { -1, 0, 0, 0, 0, 0, 0, 0, -1,0, 0, 0, 0, 0, 0, -2, 0, 0, 0 ,-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, 0, 0, 0, 0, 0, 0, 0, 0, 0}, + {-1, 0, 0, 0, 0, 0, 0, 0, -1,0, 0, 0, 0, 0, 0, 0, 0, 0, 0,-1, 0, 0, 0, 0, 0, 0, 0, -1,0, 0, 0, 0, 0, 0, 0, 0, 0, 0 } }; + + public int getTerrain(int x, int y) { + if (x < 0 || x >= field[0].length || y < 0 || y >= field.length) + return -3; + return field[y][x]; + } + + private int getTileIndex(int x, int y) { + int terrain = getTerrain(x, y); + if (terrain <= 0 && terrain >= -2) + return -terrain; + else + return 0; + } + + @Override + public int getZ() { + return -9999; + } + + @Override + public void draw(GraphicsContext gc) { + + int worldCol = 0; + int worldRow = 0; + + while(worldCol < field[0].length && worldRow < field.length) { + int worldX = worldCol*tileSize; + int worldY = worldRow*tileSize; + double screenX = worldX-gameLogic.getPlayer().getWorldX()+gameLogic.getPlayer().screenX; + double screenY = worldY-gameLogic.getPlayer().getWorldY()+gameLogic.getPlayer().screenY; + +// if(worldX>gameLogic.getPlayer().getWorldX()-gameLogic.getPlayer().screenX && +// worldXgameLogic.getPlayer().getWorldY()-gameLogic.getPlayer().screenY && +// worldY>gameLogic.getPlayer().getWorldY()+gameLogic.getPlayer().screenY ) { + if(screenX>-64 && screenX-64 && screenY < gameLogic.getGameScreen().getHeight()) { + gc.drawImage(croppedImage[Math.abs(getTileIndex(worldCol, worldRow))], screenX, screenY); + + } + + worldCol++; + + if (worldCol == field[0].length) { + worldCol = 0; + worldRow++; + } + } + } + + @Override + public boolean isDestroyed() { + // TODO Auto-generated method stub + return false; + } + + @Override + public boolean isVisible() { + // TODO Auto-generated method stub + return true; + } +} diff --git a/src/logic/field/WhiteMap.java b/src/logic/field/WhiteMap.java new file mode 100644 index 0000000..cc0c07e --- /dev/null +++ b/src/logic/field/WhiteMap.java @@ -0,0 +1,32 @@ +package logic.field; + +import javafx.scene.canvas.GraphicsContext; +import sharedObject.IRenderable; + +public class WhiteMap implements IRenderable{ + + @Override + public int getZ() { + // TODO Auto-generated method stub + return -9999; + } + + @Override + public void draw(GraphicsContext gc) { + // TODO Auto-generated method stub + gc.fillRect(0, 0, 768, 576); + } + + @Override + public boolean isDestroyed() { + // TODO Auto-generated method stub + return false; + } + + @Override + public boolean isVisible() { + // TODO Auto-generated method stub + return true; + } + +} diff --git a/src/logic/game/GameLogic.java b/src/logic/game/GameLogic.java new file mode 100644 index 0000000..7376cfe --- /dev/null +++ b/src/logic/game/GameLogic.java @@ -0,0 +1,50 @@ +package logic.game; + +import java.util.ArrayList; +import java.util.List; + +import drawing.GameScreen; +import logic.entity.Entity; +import logic.entity.Player; +import logic.entity.Werewolf; +import logic.field.Map1; +import logic.field.WhiteMap; +import sharedObject.RenderableHolder; + +public class GameLogic { + private List gameObjectContainer; + + private GameScreen gameScreen; + private Player player; + private Werewolf werewolf; + public GameLogic(GameScreen gameScreen){ + this.gameObjectContainer = new ArrayList(); + this.gameScreen = gameScreen; + player = new Player(384,288,this); + + Map1 field = new Map1(this); + RenderableHolder.getInstance().add(field); + + werewolf = new Werewolf(100,100,this); + addNewObject(player); + addNewObject(werewolf); + } + + public GameScreen getGameScreen() { + return gameScreen; + } + + protected void addNewObject(Entity entity){ + gameObjectContainer.add(entity); + RenderableHolder.getInstance().add(entity); + } + + public void logicUpdate(){ + werewolf.update(player); + player.update(); + } + + public Player getPlayer() { + return player; + } +} diff --git a/src/sharedObject/IRenderable.java b/src/sharedObject/IRenderable.java new file mode 100644 index 0000000..97e21d6 --- /dev/null +++ b/src/sharedObject/IRenderable.java @@ -0,0 +1,10 @@ +package sharedObject; + +import javafx.scene.canvas.GraphicsContext; + +public interface IRenderable { + public int getZ(); + public void draw(GraphicsContext gc); + public boolean isDestroyed(); + public boolean isVisible(); +} diff --git a/src/sharedObject/RenderableHolder.java b/src/sharedObject/RenderableHolder.java new file mode 100644 index 0000000..9f4be65 --- /dev/null +++ b/src/sharedObject/RenderableHolder.java @@ -0,0 +1,68 @@ +package sharedObject; + +import java.util.ArrayList; +import java.util.Collections; +import java.util.Comparator; +import java.util.List; + +import javafx.scene.image.Image; +import javafx.scene.media.AudioClip; +import logic.entity.Player; +import logic.entity.Werewolf; + +public class RenderableHolder { + private static final RenderableHolder instance = new RenderableHolder(); + + private List entities; + private Comparator comparator; + public static Image playerLeft; + public static Image playerRight; + public static Image johnSprite; + public static Image mapSprite; + + static { + loadResource(); + } + + public RenderableHolder() { + entities = new ArrayList(); + comparator = (IRenderable o1, IRenderable o2) -> { + if (o1.getZ() > o2.getZ()) + return 1; + return -1; + }; + } + + public static RenderableHolder getInstance() { + return instance; + } + + public static void loadResource() { + mapSprite = new Image(ClassLoader.getSystemResource("Map.png").toString()); + playerLeft = new Image(ClassLoader.getSystemResource("RabbiLeft.png").toString()); + playerRight = new Image(ClassLoader.getSystemResource("Rabbi.png").toString()); + johnSprite = new Image(ClassLoader.getSystemResource("John.png").toString()); + } + + public void add(IRenderable entity) { + System.out.println("add"); + entities.add(entity); + Collections.sort(entities, comparator); + for(IRenderable x: entities){ + if(x instanceof Werewolf) System.out.println("WereWolf"); + if(x instanceof Player) System.out.println("player"); + + } + } + + public void update() { + for (int i = entities.size() - 1; i >= 0; i--) { + if (entities.get(i).isDestroyed()) + entities.remove(i); + } + } + + public List getEntities() { + return entities; + } +}