Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -40,5 +40,5 @@ java {

application {
// Define the main class for the application.
mainClass = 'com.vincentramdhanie.twod.game.Main'
mainClass = 'com.niravramdhanie.twod.game.Main'
}
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
package com.vincentramdhanie.twod.game;
package com.niravramdhanie.twod.game;

import com.vincentramdhanie.twod.game.core.Game;
import javax.swing.SwingUtilities;

import com.niravramdhanie.twod.game.core.Game;

public class Main {
public static void main(String[] args) {
System.out.println("Starting the game application...");
Expand All @@ -15,7 +16,7 @@ public static void main(String[] args) {
try {
// Create the game
System.out.println("Creating game instance");
Game game = new Game("My 2D Game", 800, 600);
Game game = new Game("My 2D Game", 1000, 750);

// Game will start itself when initialization is complete
System.out.println("Game instance created");
Expand Down
18 changes: 18 additions & 0 deletions app/src/main/java/com/niravramdhanie/twod/game/actions/Action.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package com.niravramdhanie.twod.game.actions;

/**
* Interface for actions that can be executed when a button is pressed.
*/
public interface Action {
/**
* Executes the action.
*/
void execute();

/**
* Gets a description of the action.
*
* @return A description of what the action does
*/
String getDescription();
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,159 @@
package com.niravramdhanie.twod.game.actions;

import java.util.ArrayList;
import java.util.List;
import java.util.Random;

import com.niravramdhanie.twod.game.entity.Button;

/**
* Factory class for creating different types of actions for buttons.
*/
public class ActionFactory {
private static final Random random = new Random();

/**
* Action types enum
*/
public enum ActionType {
MESSAGE,
TIMED,
TOGGLE,
DOOR,
MULTI,
CYCLING_MESSAGE,
TIMED_TOGGLE, // A timed action that toggles
COMBINED // Multiple actions combined
}

/**
* Creates an action of the specified type.
*
* @param type The type of action to create
* @param button The button that will use this action (needed for timed actions)
* @return The created action
*/
public static Action createAction(ActionType type, Button button) {
switch (type) {
case MESSAGE:
return new MessageAction("Button pressed: " + type.name());

case CYCLING_MESSAGE:
return new MessageAction(true,
"This is message 1",
"This is message 2",
"This is message 3",
"Press again to cycle to the next message");

case TIMED:
MessageAction msgAction = new MessageAction("Timed button pressed! Will deactivate in 3 seconds.");
TimedAction timedAction = new TimedAction(msgAction, 3000);
timedAction.setTargetButton(button);
return timedAction;

case TOGGLE:
DoorAction doorAction = new DoorAction("toggle_door_" + random.nextInt(1000));
doorAction.setDoorStateChangeListener((doorId, isOpen) -> {
System.out.println("Door listener: Door " + doorId + " is now " + (isOpen ? "open" : "closed"));
});
return new ToggleAction(doorAction, doorAction);

case DOOR:
DoorAction simpleDoor = new DoorAction("simple_door_" + random.nextInt(1000));
return simpleDoor;

case MULTI:
// Create a multi-action with several different actions
MessageAction firstAction = new MessageAction("First action executed!");
MessageAction secondAction = new MessageAction("Second action executed!");
DoorAction thirdAction = new DoorAction("multi_door_" + random.nextInt(1000));
thirdAction.setDoorStateChangeListener((doorId, isOpen) -> {
System.out.println("MultiAction door: " + doorId + " is now " + (isOpen ? "open" : "closed"));
});

return new MultiAction(firstAction, secondAction, thirdAction);

case TIMED_TOGGLE:
// Create a timed action that wraps a toggle action
ToggleAction toggleAction = new ToggleAction(
new MessageAction("Toggled OFF"),
new MessageAction("Toggled ON")
);
TimedAction timedToggle = new TimedAction(toggleAction, 5000);
timedToggle.setTargetButton(button);
return timedToggle;

case COMBINED:
// Create a combination of different action types
// This example creates a multi-action with cycling messages and a door action
MessageAction cyclingMsg = new MessageAction(true,
"Combined action - Message 1",
"Combined action - Message 2",
"Combined action - Message 3");

DoorAction combinedDoor = new DoorAction("combined_door_" + random.nextInt(1000));
combinedDoor.setDoorStateChangeListener((doorId, isOpen) -> {
System.out.println("Combined door: " + doorId + " is now " + (isOpen ? "open" : "closed"));
});

return new MultiAction(cyclingMsg, combinedDoor);

default:
return new MessageAction("Default action");
}
}

/**
* Creates a random action.
*
* @param button The button that will use this action
* @return A randomly selected action
*/
public static Action createRandomAction(Button button) {
ActionType[] types = ActionType.values();
ActionType randomType = types[random.nextInt(types.length)];

return createAction(randomType, button);
}

/**
* Creates one of each action type.
*
* @param button The button that will use these actions (for timed actions)
* @return A list containing one of each action type
*/
public static List<Action> createAllActionTypes(Button button) {
List<Action> actions = new ArrayList<>();

for (ActionType type : ActionType.values()) {
actions.add(createAction(type, button));
}

return actions;
}

/**
* Creates a timed message action.
*
* @param message The message to display
* @param durationMillis The duration in milliseconds
* @param button The button to deactivate
* @return A timed message action
*/
public static TimedAction createTimedMessage(String message, int durationMillis, Button button) {
MessageAction msgAction = new MessageAction(message);
TimedAction timedAction = new TimedAction(msgAction, durationMillis);
timedAction.setTargetButton(button);
return timedAction;
}

/**
* Creates a cycling message action.
*
* @param messages The messages to cycle through
* @return A cycling message action
*/
public static MessageAction createCyclingMessage(String... messages) {
return new MessageAction(true, messages);
}
}
104 changes: 104 additions & 0 deletions app/src/main/java/com/niravramdhanie/twod/game/actions/DoorAction.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
package com.niravramdhanie.twod.game.actions;

/**
* An action that simulates opening or closing a door.
*/
public class DoorAction implements Action {
private String doorId;
private boolean doorOpen;
private DoorStateChangeListener listener;

/**
* Creates a new door action.
*
* @param doorId The ID of the door
*/
public DoorAction(String doorId) {
this.doorId = doorId;
this.doorOpen = false;
this.listener = null;
}

@Override
public void execute() {
// Toggle the door state
doorOpen = !doorOpen;

// Notify the listener if present
if (listener != null) {
listener.onDoorStateChanged(doorId, doorOpen);
}

// Print a message to the console
System.out.println("Door " + doorId + " is now " + (doorOpen ? "open" : "closed"));
}

@Override
public String getDescription() {
return "Door '" + doorId + "' - " + (doorOpen ? "OPEN" : "CLOSED");
}

/**
* Interface for components that need to be notified of door state changes.
*/
public interface DoorStateChangeListener {
/**
* Called when a door's state changes.
*
* @param doorId The ID of the door
* @param isOpen True if the door is now open, false if closed
*/
void onDoorStateChanged(String doorId, boolean isOpen);
}

/**
* Sets the listener for door state changes.
*
* @param listener The listener to notify when the door state changes
*/
public void setDoorStateChangeListener(DoorStateChangeListener listener) {
this.listener = listener;
}

/**
* Gets the door ID.
*
* @return The door ID
*/
public String getDoorId() {
return doorId;
}

/**
* Sets the door ID.
*
* @param doorId The door ID
*/
public void setDoorId(String doorId) {
this.doorId = doorId;
}

/**
* Checks if the door is open.
*
* @return True if the door is open, false if closed
*/
public boolean isDoorOpen() {
return doorOpen;
}

/**
* Sets the door state.
*
* @param doorOpen True to set as open, false for closed
*/
public void setDoorOpen(boolean doorOpen) {
boolean oldState = this.doorOpen;
this.doorOpen = doorOpen;

// If the state changed and we have a listener, notify it
if (oldState != doorOpen && listener != null) {
listener.onDoorStateChanged(doorId, doorOpen);
}
}
}
Loading