Skip to content

Commit

Permalink
2d game with fixing camera
Browse files Browse the repository at this point in the history
  • Loading branch information
PacharaphonP committed Apr 16, 2023
1 parent d49205f commit 5de5f53
Show file tree
Hide file tree
Showing 36 changed files with 619 additions and 11 deletions.
1 change: 1 addition & 0 deletions .classpath
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,6 @@
</attributes>
</classpathentry>
<classpathentry kind="src" path="src"/>
<classpathentry kind="src" path="res"/>
<classpathentry kind="output" path="bin"/>
</classpath>
Binary file added bin/John.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added bin/Map.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added bin/Rabbi.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added bin/RabbiLeft.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added bin/application/Main$1.class
Binary file not shown.
Binary file modified bin/application/Main.class
Binary file not shown.
Binary file added bin/drawing/GameScreen.class
Binary file not shown.
Binary file added bin/input/InputUtility.class
Binary file not shown.
Binary file removed bin/logic/charClass/BaseCharClass.class
Binary file not shown.
Binary file removed bin/logic/entity/BaseEntity.class
Binary file not shown.
Binary file added bin/logic/entity/Entity.class
Binary file not shown.
Binary file added bin/logic/entity/Player.class
Binary file not shown.
Binary file added bin/logic/entity/Werewolf.class
Binary file not shown.
Binary file added bin/logic/field/Map1.class
Binary file not shown.
Binary file added bin/logic/field/WhiteMap.class
Binary file not shown.
Binary file added bin/logic/game/GameLogic.class
Binary file not shown.
Binary file added bin/sharedObject/IRenderable.class
Binary file not shown.
Binary file added bin/sharedObject/RenderableHolder.class
Binary file not shown.
Binary file added res/John.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added res/Map.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added res/Rabbi.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added res/RabbiLeft.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
54 changes: 53 additions & 1 deletion src/application/Main.java
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();
}

}
74 changes: 74 additions & 0 deletions src/drawing/GameScreen.java
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);
}
}
}
}
45 changes: 45 additions & 0 deletions src/input/InputUtility.java
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;
}

}
5 changes: 0 additions & 5 deletions src/logic/charClass/BaseCharClass.java

This file was deleted.

5 changes: 0 additions & 5 deletions src/logic/entity/BaseEntity.java

This file was deleted.

49 changes: 49 additions & 0 deletions src/logic/entity/Entity.java
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;
}
}
67 changes: 67 additions & 0 deletions src/logic/entity/Player.java
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()) {

}
}

}
52 changes: 52 additions & 0 deletions src/logic/entity/Werewolf.java
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

}

}
Loading

0 comments on commit 5de5f53

Please sign in to comment.