Skip to content

Commit

Permalink
(Fixed #3, Fixed #11, Fixed #12, Fixed #13) Added dynamic player move…
Browse files Browse the repository at this point in the history
…ment, basic fonts, basic buttons (no text yet) and added support for different resolutions
  • Loading branch information
Tim Anema committed Sep 4, 2016
1 parent 3d04a6a commit b22570d
Show file tree
Hide file tree
Showing 40 changed files with 464 additions and 130 deletions.
13 changes: 10 additions & 3 deletions src/main/java/me/timefall/timefall/GameState.java
Original file line number Diff line number Diff line change
@@ -1,17 +1,24 @@
package me.timefall.timefall;

import me.timefall.timefall.graphics.Screen;
import me.timefall.timefall.graphics.components.Screen;

public abstract class GameState
{
Settings settings;
private Settings settings;
private Screen screen;

public GameState(Settings settings)
public GameState(Settings settings, Screen screen)
{
this.settings = settings;
this.screen = screen;
}

public abstract void tick(double deltaTime);

public abstract void render(Screen screen);

public Screen getScreen()
{
return this.screen;
}
}
32 changes: 19 additions & 13 deletions src/main/java/me/timefall/timefall/Settings.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package me.timefall.timefall;

import java.awt.*;
import java.util.Stack;

public class Settings
Expand All @@ -16,25 +17,23 @@ public class Settings

public enum ScreenSize
{
SMALL(640, 360, false, 0, 1),
MEDIUM(960, 540, false, 1, 1.5f),
NORMAL(1280, 720, false, 2, 2),
LARGE(1920, 1080, false, 3, 3),
FULLSCREEN(1920, 1080, true, 4, 3);
SMALL(640, 360, false, 0),
MEDIUM(960, 540, false, 1),
NORMAL(1280, 720, false, 2),
LARGE(1920, 1080, false, 3),
FULLSCREEN(1920, 1080, true, 4);

private final int width;
private final int height;
private final float scale;

private final boolean fullscreen;

private final int ID;

private ScreenSize(int width, int height, boolean fullscreen, int ID, float scale)
private ScreenSize(int width, int height, boolean fullscreen, int ID)
{
this.width = width;
this.height = height;
this.scale = scale;
this.fullscreen = fullscreen;
this.ID = ID;
}
Expand All @@ -49,11 +48,6 @@ public int getHeight()
return height;
}

public float getScale()
{
return scale;
}

public int getID()
{
return ID;
Expand Down Expand Up @@ -114,6 +108,8 @@ public void setState(GameState gameState)
}

states.push(gameState);

EventQueue.invokeLater(() -> Timefall.getMainDisplay().getGameCanvas().updateScreen(gameState.getScreen()));
}

public void removeState(GameState gameState)
Expand Down Expand Up @@ -168,6 +164,16 @@ public GameState getCurrentState()
return states.peek();
}

public float getScale()
{
int screenWidth = Timefall.getMainDisplay().getScreen().width, screenHeight = Timefall.getMainDisplay().getScreen().height;
int windowWidth = this.getScreenSize().width, windowHeight = this.getScreenSize().height;

float xScale = windowWidth / screenWidth, yScale = windowHeight / screenHeight;

return xScale == yScale ? xScale : 1;
}

public boolean isLightEnabled()
{
return lightEnabled;
Expand Down
30 changes: 22 additions & 8 deletions src/main/java/me/timefall/timefall/Timefall.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@
import me.timefall.timefall.events.mouse.MouseHandler;
import me.timefall.timefall.files.FileManager;
import me.timefall.timefall.game.game.Game;
import me.timefall.timefall.game.titlescreen.TitleScreen;
import me.timefall.timefall.graphics.components.Screen;
import me.timefall.timefall.graphics.handlers.ButtonHandler;
import me.timefall.timefall.level.TileManager;
import me.timefall.timefall.threads.ThreadManager;

Expand All @@ -14,8 +17,10 @@ public class Timefall
{
private Object lockObject;

public static final int X_RES = 640;
public static final int Y_RES = 360;
public static int GAME_X_RES = 320; // 640
public static int GAME_Y_RES = 180; // 360
public static final int MENU_X_RES = 640;
public static final int MENU_Y_RES = 360;

private static volatile Display mainDisplay;
private static Settings settings;
Expand All @@ -24,6 +29,7 @@ public class Timefall
private static MouseHandler mouseHandler;
private static ThreadManager threadManager;
private static TileManager tileManager;
private static ButtonHandler buttonHandler;

public static void main(String args[])
{
Expand All @@ -39,6 +45,9 @@ public Timefall()
keyHandler = new KeyHandler();
mouseHandler = new MouseHandler();

System.out.println(" Reading files and readjusting settings ...");
fileManager = new FileManager(settings);

System.out.println(" Initializing display ...");

EventQueue.invokeLater(() -> mainDisplay = new Display("Timefall", settings.getScreenSize().getWidth(), settings.getScreenSize().getHeight(), lockObject));
Expand All @@ -64,16 +73,16 @@ private void lock()

private void internalStart()
{
System.out.println(" Reading files and readjusting settings ...");
fileManager = new FileManager(settings);
System.out.println(" Initializing game threads ...");
buttonHandler = new ButtonHandler();

settings.setState(new TitleScreen(settings, new Screen(MENU_X_RES, MENU_Y_RES)));
// Add temp gamestate
// TODO: Remove this
startGame();

System.out.println(" Initializing game threads ...");
//startGame();

// Start main thread
System.out.println(" Initializing game threads ...");
Timefall.initThreads();

System.out.println("All Timefall components loaded!");
Expand All @@ -83,7 +92,7 @@ private void internalStart()
public static void startGame()
{
//TODO: Verander settings later nog vanuit files
Game game = new Game(settings);
Game game = new Game(settings, new Screen(GAME_X_RES, GAME_Y_RES));
settings.setState(game);

tileManager = game.tileManager;
Expand Down Expand Up @@ -129,4 +138,9 @@ public static TileManager getTileManager()
{
return tileManager;
}

public static ButtonHandler getButtonHandler()
{
return buttonHandler;
}
}
12 changes: 4 additions & 8 deletions src/main/java/me/timefall/timefall/display/Display.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

import me.timefall.timefall.Timefall;
import me.timefall.timefall.game.game.MoveListener;
import me.timefall.timefall.graphics.Screen;
import me.timefall.timefall.graphics.components.Screen;

import javax.imageio.ImageIO;
import javax.swing.*;
Expand All @@ -23,7 +23,6 @@ public class Display

public JFrame jFrame;
private GamePanel gameCanvas;
private Screen screen;

public Display(String gameName, int width, int height, Object lockObject)
{
Expand Down Expand Up @@ -79,8 +78,7 @@ public void windowClosing(WindowEvent e)
});

// Create components
screen = new Screen(Timefall.X_RES, Timefall.Y_RES);
gameCanvas = new GamePanel(width, height, dimension, screen, screen.bufferedImage);
gameCanvas = new GamePanel(width, height, dimension);

// Add components
jFrame.add(gameCanvas);
Expand All @@ -101,8 +99,6 @@ public void windowClosing(WindowEvent e)
jFrame.pack();
jFrame.setVisible(true);

gameCanvas.initBuffer();

// Add listeners
jFrame.addKeyListener(Timefall.getKeyHandler());
jFrame.addFocusListener(Timefall.getKeyHandler());
Expand Down Expand Up @@ -146,11 +142,11 @@ private void onClose()

public GamePanel getGameCanvas()
{
return gameCanvas;
return this.gameCanvas;
}

public Screen getScreen()
{
return screen;
return this.gameCanvas.getScreen();
}
}
30 changes: 26 additions & 4 deletions src/main/java/me/timefall/timefall/display/GamePanel.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package me.timefall.timefall.display;

import me.timefall.timefall.Timefall;
import me.timefall.timefall.graphics.Screen;
import me.timefall.timefall.graphics.components.Screen;

import java.awt.*;
import java.awt.image.BufferStrategy;
Expand All @@ -18,13 +18,11 @@ public class GamePanel extends Canvas
private Dimension dimension;
private Screen screen;

public GamePanel(int width, int height, Dimension dimension, Screen screen, BufferedImage bufferedImage)
public GamePanel(int width, int height, Dimension dimension)
{
this.width = width;
this.height = height;
this.dimension = dimension;
this.screen = screen;
this.bufferedImage = bufferedImage;

initComponents();
}
Expand All @@ -44,15 +42,38 @@ private void initComponents()

public void initBuffer()
{
if (screen == null)
{
return;
}

createBufferStrategy(2);

bufferStrategy = getBufferStrategy();
graphics = bufferStrategy.getDrawGraphics();
}

public void updateScreen(Screen screen)
{
this.screen = screen;
this.bufferedImage = screen.bufferedImage;
this.initBuffer();
}

public Screen getScreen()
{
return this.screen;
}

public void render()
{
if (screen == null)
{
return;
}

Timefall.getSettings().getCurrentState().render(screen);
Timefall.getButtonHandler().renderButtons(screen);

// Render colours on screen
screen.render();
Expand All @@ -65,6 +86,7 @@ public void render()

// Dispose of the current graphics and issue the new buffer
//graphics.dispose();

bufferStrategy.show();
}
}
4 changes: 2 additions & 2 deletions src/main/java/me/timefall/timefall/entities/Entity.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package me.timefall.timefall.entities;

import me.timefall.timefall.graphics.Bitmap;
import me.timefall.timefall.graphics.Screen;
import me.timefall.timefall.graphics.components.Bitmap;
import me.timefall.timefall.graphics.components.Screen;
import me.timefall.timefall.level.Direction;
import me.timefall.timefall.level.Vector;
import me.timefall.timefall.level.world.World;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package me.timefall.timefall.entities;

import me.timefall.timefall.Timefall;
import me.timefall.timefall.graphics.Screen;
import me.timefall.timefall.graphics.components.Screen;
import me.timefall.timefall.level.Direction;
import me.timefall.timefall.level.Vector;

Expand Down
2 changes: 2 additions & 0 deletions src/main/java/me/timefall/timefall/entities/Mob.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,6 @@ public interface Mob extends Entity

public boolean canMove(Direction direction);

public void moveAnimationToggle();

}
Loading

0 comments on commit b22570d

Please sign in to comment.