Skip to content

Commit

Permalink
Add game modes
Browse files Browse the repository at this point in the history
  • Loading branch information
Kiyotoko committed May 19, 2024
1 parent 331d25f commit c99acfa
Show file tree
Hide file tree
Showing 10 changed files with 210 additions and 93 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
/*/
!src
config.ini

# Compiled class file
*.class
Expand Down
16 changes: 11 additions & 5 deletions src/main/java/org/seekers/App.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,14 @@
import org.ini4j.Ini;
import org.pf4j.JarPluginManager;
import org.pf4j.PluginManager;
import org.seekers.game.StandardMode;
import org.seekers.game.Tournament;
import org.seekers.grpc.SeekersServer;

import javafx.application.Application;
import javafx.stage.Stage;
import org.seekers.plugin.LanguageLoader;
import org.seekers.plugin.ClientLoader;
import org.seekers.plugin.GameMode;
import org.seekers.plugin.SeekersExtension;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
Expand All @@ -49,7 +52,8 @@ public class App extends Application {
private static final Logger logger = LoggerFactory.getLogger(App.class);

private final @Nonnull PluginManager manager = new JarPluginManager();
private final @Nonnull List<LanguageLoader> loaders = new ArrayList<>();
private final @Nonnull List<ClientLoader> loaders = new ArrayList<>();
private final @Nonnull List<GameMode> modes = new ArrayList<>();
private final @Nonnull Ini config = new Ini();

/**
Expand Down Expand Up @@ -87,13 +91,16 @@ public void init() throws IOException {
logger.info("Found following extensions: {}", extensions);
for (SeekersExtension extension : extensions) {
extension.setup(config.get(manager.whichPlugin(extension.getClass()).getPluginId()));
extension.addLanguageLoaders(loaders);
extension.addClientLoaders(loaders);
extension.addGameModes(modes);
}
}

@Override
public void start(Stage stage) throws Exception {
final SeekersServer server = new SeekersServer(stage, config, loaders);
final SeekersServer server = new SeekersServer(stage, config).setGameMode(new StandardMode())
.setTournament(new Tournament("players")).addClientLoaders(loaders);
server.start();
stage.setOnCloseRequest(c -> {
logger.info("Try unloading plugins and stopping server on stage close request");
manager.stopPlugins();
Expand All @@ -104,7 +111,6 @@ public void start(Stage stage) throws Exception {
Thread.currentThread().interrupt();
}
});
stage.setScene(server.getGame());
stage.setTitle("Seekers");
stage.setAlwaysOnTop(true);
stage.setResizable(false);
Expand Down
85 changes: 38 additions & 47 deletions src/main/java/org/seekers/game/Game.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@
import javafx.animation.Timeline;
import javafx.beans.property.BooleanProperty;
import javafx.beans.property.SimpleBooleanProperty;
import javafx.geometry.Point2D;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.scene.SceneAntialiasing;
Expand All @@ -37,8 +36,8 @@
import org.seekers.grpc.Corresponding;
import org.seekers.grpc.service.CommandResponse;
import org.seekers.plugin.GameMap;
import org.seekers.plugin.Tournament;

import javax.annotation.CheckReturnValue;
import javax.annotation.Nonnull;
import java.util.*;

Expand All @@ -54,7 +53,7 @@ public class Game extends Scene {

// Game objects
private final @Nonnull List<Entity> entities = new ArrayList<>();
private final GameMap gameMap;
private GameMap gameMap;
private long tick = 0;

// Cached types for gRPC fast access
Expand Down Expand Up @@ -87,7 +86,6 @@ public Game(@Nonnull BorderPane parent, @Nonnull Game.Properties gameProperties,
this.campProperties = campProperties;
this.seekerProperties = seekerProperties;
this.goalProperties = goalProperties;
this.gameMap = new TorusMap(gameProperties.width, gameProperties.height);

time.setFont(Font.font("Ubuntu", 14));
time.setTextFill(Color.WHITESMOKE);
Expand All @@ -99,7 +97,6 @@ public Game(@Nonnull BorderPane parent, @Nonnull Game.Properties gameProperties,

Timeline timeline = getTimeline();
timeline.play();
addGoals();
}

/**
Expand All @@ -114,7 +111,6 @@ public static class Properties {
* @param ini the ini object that holds the data of {@code config.ini}
*/
public Properties(Ini ini) {
autoPlay = ini.fetch(SECTION, "auto-play", boolean.class);
playtime = ini.fetch(SECTION, "playtime", int.class);
players = ini.fetch(SECTION, "players", int.class);
seekers = ini.fetch(SECTION, "seekers", int.class);
Expand All @@ -124,15 +120,14 @@ public Properties(Ini ini) {
}

// Global properties
private final boolean autoPlay;
private final int playtime;
private final int players;
private final int seekers;
private final int goals;
final int playtime;
final int players;
final int seekers;
final int goals;

// Map properties
private final double width;
private final double height;
final double width;
final double height;
}

/**
Expand All @@ -152,9 +147,6 @@ private Timeline getTimeline() {
}
for (Entity entity : List.copyOf(getEntities())) {
entity.update();
if (gameProperties.autoPlay && (entity instanceof Seeker)) {
((Seeker) entity).setAutoCommands();
}
}
time.setText("[ " + (++tick) + " ]");
}));
Expand All @@ -175,16 +167,18 @@ public synchronized void reset() {
entities.clear();
players.clear();
seekers.clear();
goals.clear();
camps.clear();

// Clear scene content
getBack().getChildren().clear();
getFront().getChildren().clear();
getInfo().getChildren().clear();

// Add goals back to game
addGoals();
// Add goals back
for (Goal goal : goals) {
getFront().getChildren().add(goal);
getEntities().add(goal);
}

// Reset property
finished.set(false);
Expand All @@ -206,34 +200,6 @@ public void addToTournament(Tournament tournament) {
}
}

/**
* Adds a new player to the game environment. Creates a camp and a specified
* number of seekers for the player.
*
* @return the newly added player
*/
public Player addPlayer() {
Player player = new Player(this);
Camp camp = new Camp(player, campProperties);
camp.setPosition(new Point2D(gameProperties.width * (players.size() - 0.5) / gameProperties.players,
gameProperties.height * 0.5));
for (int s = 0; s < gameProperties.seekers; s++) {
Seeker seeker = new Seeker(player, seekerProperties);
seeker.setPosition(getGameMap().getRandomPosition());
}
return player;
}

/**
* Adds a specified number of goals to the game environment at random positions.
*/
public void addGoals() {
for (int i = 0; i < gameProperties.goals; i++) {
Goal goal = new Goal(this, goalProperties);
goal.setPosition(getGameMap().getRandomPosition());
}
}

/**
* @return the current status of the game
*/
Expand Down Expand Up @@ -285,10 +251,15 @@ public List<Camp> getCamps() {
return camps;
}

@CheckReturnValue
public GameMap getGameMap() {
return gameMap;
}

public void setGameMap(@Nonnull GameMap gameMap) {
this.gameMap = gameMap;
}

@Nonnull
public BooleanProperty finishedProperty() {
return finished;
Expand All @@ -315,4 +286,24 @@ public Group getFront() {
public long getPassedPlaytime() {
return tick;
}

@Nonnull
public Properties getGameProperties() {
return gameProperties;
}

@Nonnull
public Seeker.Properties getSeekerProperties() {
return seekerProperties;
}

@Nonnull
public Goal.Properties getGoalProperties() {
return goalProperties;
}

@Nonnull
public Camp.Properties getCampProperties() {
return campProperties;
}
}
66 changes: 66 additions & 0 deletions src/main/java/org/seekers/game/StandardMode.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
/*
* Copyright (C) 2022 Seekers Contributors
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/

package org.seekers.game;

import javafx.geometry.Point2D;
import javafx.scene.layout.BorderPane;
import org.seekers.plugin.GameMap;
import org.seekers.plugin.GameMode;

public class StandardMode implements GameMode {

@Override
public Game createGame(Game.Properties gameProperties, Camp.Properties campProperties,
Seeker.Properties seekerProperties, Goal.Properties goalProperties) {
Game game = new Game(new BorderPane(), gameProperties, campProperties, seekerProperties, goalProperties);
game.setGameMap(createGameMap(game));
for (int i = 0; i < game.getGameProperties().goals; i++)
createGoal(game).setPosition(game.getGameMap().getRandomPosition());
return game;
}

@Override
public GameMap createGameMap(Game game) {
return new TorusMap(game.getGameProperties().width, game.getGameProperties().height);
}

@Override
public Player createPlayer(Game game) {
Player player = new Player(game);
createCamp(player).setPosition(new Point2D(game.getGameProperties().width * (game.getPlayers().size() - 0.5)
/ game.getGameProperties().players, game.getGameProperties().height * 0.5));
for (int i = 0; i < game.getGameProperties().seekers; i++)
createSeeker(player).setPosition(game.getGameMap().getRandomPosition());
return player;
}

@Override
public Camp createCamp(Player player) {
return new Camp(player, player.getGame().getCampProperties());
}

@Override
public Seeker createSeeker(Player player) {
return new Seeker(player, player.getGame().getSeekerProperties());
}

@Override
public Goal createGoal(Game game) {
return new Goal(game, game.getGoalProperties());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/

package org.seekers.plugin;
package org.seekers.game;

import com.google.gson.Gson;
import org.slf4j.Logger;
Expand Down
Loading

0 comments on commit c99acfa

Please sign in to comment.