-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
d49205f
commit 5de5f53
Showing
36 changed files
with
619 additions
and
11 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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(); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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); | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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<KeyCode> 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; | ||
} | ||
|
||
} |
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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()) { | ||
|
||
} | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 | ||
|
||
} | ||
|
||
} |
Oops, something went wrong.