From 8bc296f1bc3a5ac8260ca24383de270e1ea9260c Mon Sep 17 00:00:00 2001 From: Nirav Ramdhanie Date: Wed, 7 May 2025 21:51:48 -0700 Subject: [PATCH 1/8] fixes movement and renames package --- app/build.gradle | 2 +- .../twod/game/Main.java | 5 +- .../twod/game/core/Game.java | 8 +- .../twod/game/core/GamePanel.java | 11 ++- .../twod/game/core/GameStateManager.java | 16 ++-- .../twod/game/entity/BallPlayer.java | 96 +++++++++---------- .../twod/game/entity/Block.java | 7 +- .../twod/game/entity/Entity.java | 6 +- .../twod/game/graphics/Animation.java | 2 +- .../twod/game/graphics/SpriteSheet.java | 2 +- .../twod/game/input/KeyHandler.java | 6 +- .../twod/game/input/MouseHandler.java | 6 +- .../twod/game/state/GameState.java | 6 +- .../twod/game/state/MenuState.java | 10 +- .../twod/game/state/PauseState.java | 8 +- .../twod/game/state/PlayState.java | 12 +-- .../twod/game/ui/Button.java | 3 +- .../twod/game/ui/HUD.java | 4 +- .../twod/game/ui/UIComponent.java | 4 +- .../twod/game/utils/ResourceLoader.java | 5 +- .../twod/game/utils/Vector2D.java | 2 +- 21 files changed, 106 insertions(+), 115 deletions(-) rename app/src/main/java/com/{vincentramdhanie => niravramdhanie}/twod/game/Main.java (90%) rename app/src/main/java/com/{vincentramdhanie => niravramdhanie}/twod/game/core/Game.java (97%) rename app/src/main/java/com/{vincentramdhanie => niravramdhanie}/twod/game/core/GamePanel.java (99%) rename app/src/main/java/com/{vincentramdhanie => niravramdhanie}/twod/game/core/GameStateManager.java (94%) rename app/src/main/java/com/{vincentramdhanie => niravramdhanie}/twod/game/entity/BallPlayer.java (84%) rename app/src/main/java/com/{vincentramdhanie => niravramdhanie}/twod/game/entity/Block.java (93%) rename app/src/main/java/com/{vincentramdhanie => niravramdhanie}/twod/game/entity/Entity.java (93%) rename app/src/main/java/com/{vincentramdhanie => niravramdhanie}/twod/game/graphics/Animation.java (97%) rename app/src/main/java/com/{vincentramdhanie => niravramdhanie}/twod/game/graphics/SpriteSheet.java (98%) rename app/src/main/java/com/{vincentramdhanie => niravramdhanie}/twod/game/input/KeyHandler.java (90%) rename app/src/main/java/com/{vincentramdhanie => niravramdhanie}/twod/game/input/MouseHandler.java (94%) rename app/src/main/java/com/{vincentramdhanie => niravramdhanie}/twod/game/state/GameState.java (83%) rename app/src/main/java/com/{vincentramdhanie => niravramdhanie}/twod/game/state/MenuState.java (97%) rename app/src/main/java/com/{vincentramdhanie => niravramdhanie}/twod/game/state/PauseState.java (95%) rename app/src/main/java/com/{vincentramdhanie => niravramdhanie}/twod/game/state/PlayState.java (96%) rename app/src/main/java/com/{vincentramdhanie => niravramdhanie}/twod/game/ui/Button.java (98%) rename app/src/main/java/com/{vincentramdhanie => niravramdhanie}/twod/game/ui/HUD.java (96%) rename app/src/main/java/com/{vincentramdhanie => niravramdhanie}/twod/game/ui/UIComponent.java (94%) rename app/src/main/java/com/{vincentramdhanie => niravramdhanie}/twod/game/utils/ResourceLoader.java (98%) rename app/src/main/java/com/{vincentramdhanie => niravramdhanie}/twod/game/utils/Vector2D.java (93%) diff --git a/app/build.gradle b/app/build.gradle index 75d9dc6..2866ff8 100644 --- a/app/build.gradle +++ b/app/build.gradle @@ -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' } diff --git a/app/src/main/java/com/vincentramdhanie/twod/game/Main.java b/app/src/main/java/com/niravramdhanie/twod/game/Main.java similarity index 90% rename from app/src/main/java/com/vincentramdhanie/twod/game/Main.java rename to app/src/main/java/com/niravramdhanie/twod/game/Main.java index 9019ee5..30d25ad 100644 --- a/app/src/main/java/com/vincentramdhanie/twod/game/Main.java +++ b/app/src/main/java/com/niravramdhanie/twod/game/Main.java @@ -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..."); diff --git a/app/src/main/java/com/vincentramdhanie/twod/game/core/Game.java b/app/src/main/java/com/niravramdhanie/twod/game/core/Game.java similarity index 97% rename from app/src/main/java/com/vincentramdhanie/twod/game/core/Game.java rename to app/src/main/java/com/niravramdhanie/twod/game/core/Game.java index 8df1146..9e32cb7 100644 --- a/app/src/main/java/com/vincentramdhanie/twod/game/core/Game.java +++ b/app/src/main/java/com/niravramdhanie/twod/game/core/Game.java @@ -1,12 +1,12 @@ -package com.vincentramdhanie.twod.game.core; - -import com.vincentramdhanie.twod.game.input.KeyHandler; -import com.vincentramdhanie.twod.game.input.MouseHandler; +package com.niravramdhanie.twod.game.core; import javax.swing.JFrame; import javax.swing.SwingUtilities; import javax.swing.Timer; +import com.niravramdhanie.twod.game.input.KeyHandler; +import com.niravramdhanie.twod.game.input.MouseHandler; + public class Game implements Runnable { private JFrame window; private GamePanel gamePanel; diff --git a/app/src/main/java/com/vincentramdhanie/twod/game/core/GamePanel.java b/app/src/main/java/com/niravramdhanie/twod/game/core/GamePanel.java similarity index 99% rename from app/src/main/java/com/vincentramdhanie/twod/game/core/GamePanel.java rename to app/src/main/java/com/niravramdhanie/twod/game/core/GamePanel.java index 8100acc..9b30bbd 100644 --- a/app/src/main/java/com/vincentramdhanie/twod/game/core/GamePanel.java +++ b/app/src/main/java/com/niravramdhanie/twod/game/core/GamePanel.java @@ -1,16 +1,17 @@ -package com.vincentramdhanie.twod.game.core; +package com.niravramdhanie.twod.game.core; -import javax.swing.JPanel; +import java.awt.Color; import java.awt.Dimension; +import java.awt.Font; import java.awt.Graphics; import java.awt.Graphics2D; -import java.awt.Color; -import java.awt.Font; -import java.awt.RenderingHints; import java.awt.GraphicsConfiguration; import java.awt.GraphicsEnvironment; +import java.awt.RenderingHints; import java.awt.image.BufferedImage; +import javax.swing.JPanel; + public class GamePanel extends JPanel { private BufferedImage image; private Graphics2D g2d; diff --git a/app/src/main/java/com/vincentramdhanie/twod/game/core/GameStateManager.java b/app/src/main/java/com/niravramdhanie/twod/game/core/GameStateManager.java similarity index 94% rename from app/src/main/java/com/vincentramdhanie/twod/game/core/GameStateManager.java rename to app/src/main/java/com/niravramdhanie/twod/game/core/GameStateManager.java index 1bcd43b..c8c6f29 100644 --- a/app/src/main/java/com/vincentramdhanie/twod/game/core/GameStateManager.java +++ b/app/src/main/java/com/niravramdhanie/twod/game/core/GameStateManager.java @@ -1,16 +1,16 @@ -package com.vincentramdhanie.twod.game.core; - -import com.vincentramdhanie.twod.game.state.GameState; -import com.vincentramdhanie.twod.game.state.MenuState; -import com.vincentramdhanie.twod.game.state.PlayState; -import com.vincentramdhanie.twod.game.state.PauseState; -import com.vincentramdhanie.twod.game.input.KeyHandler; -import com.vincentramdhanie.twod.game.input.MouseHandler; +package com.niravramdhanie.twod.game.core; import java.awt.Graphics2D; import java.util.ArrayList; import java.util.List; +import com.niravramdhanie.twod.game.input.KeyHandler; +import com.niravramdhanie.twod.game.input.MouseHandler; +import com.niravramdhanie.twod.game.state.GameState; +import com.niravramdhanie.twod.game.state.MenuState; +import com.niravramdhanie.twod.game.state.PauseState; +import com.niravramdhanie.twod.game.state.PlayState; + public class GameStateManager { private List gameStates; private int currentState; diff --git a/app/src/main/java/com/vincentramdhanie/twod/game/entity/BallPlayer.java b/app/src/main/java/com/niravramdhanie/twod/game/entity/BallPlayer.java similarity index 84% rename from app/src/main/java/com/vincentramdhanie/twod/game/entity/BallPlayer.java rename to app/src/main/java/com/niravramdhanie/twod/game/entity/BallPlayer.java index 1228ba8..9430538 100644 --- a/app/src/main/java/com/vincentramdhanie/twod/game/entity/BallPlayer.java +++ b/app/src/main/java/com/niravramdhanie/twod/game/entity/BallPlayer.java @@ -1,14 +1,14 @@ -package com.vincentramdhanie.twod.game.entity; +package com.niravramdhanie.twod.game.entity; -import com.vincentramdhanie.twod.game.graphics.Animation; -import com.vincentramdhanie.twod.game.graphics.SpriteSheet; -import com.vincentramdhanie.twod.game.utils.ResourceLoader; - -import java.awt.Graphics2D; import java.awt.Color; +import java.awt.Graphics2D; import java.awt.image.BufferedImage; import java.util.List; +import com.niravramdhanie.twod.game.graphics.Animation; +import com.niravramdhanie.twod.game.graphics.SpriteSheet; +import com.niravramdhanie.twod.game.utils.ResourceLoader; + public class BallPlayer extends Entity { // Movement flags private boolean left; @@ -19,7 +19,6 @@ public class BallPlayer extends Entity { // Movement properties private float moveSpeed; private float maxSpeed; - private float friction; // Health properties private int health; @@ -46,10 +45,9 @@ public BallPlayer(float x, float y, int width, int height, int screenWidth, int this.screenWidth = screenWidth; this.screenHeight = screenHeight; - // Set movement properties - moveSpeed = 0.5f; - maxSpeed = 5.0f; - friction = 0.1f; + // Set movement properties - instant movement with no friction + moveSpeed = 0.84f; // 1.2f * 0.7 = 0.84f + maxSpeed = 2.8f; // 4.0f * 0.7 = 2.8f // Set health properties this.maxHealth = 100; @@ -129,31 +127,44 @@ public void update() { // Track previous animation for transition checks Animation previousAnim = currentAnim; - // Handle movement - // Apply acceleration based on input - if (left) { - velocity.x -= moveSpeed; - if (velocity.x < -maxSpeed) velocity.x = -maxSpeed; - if (spritesLoaded) currentAnim = leftAnim; - } - if (right) { - velocity.x += moveSpeed; - if (velocity.x > maxSpeed) velocity.x = maxSpeed; - if (spritesLoaded) currentAnim = rightAnim; + // Reset velocity immediately when no keys are pressed for instant stopping + if (!left && !right) velocity.x = 0; + if (!up && !down) velocity.y = 0; + + // Calculate input direction vector + float dirX = 0, dirY = 0; + if (left) dirX -= 1; + if (right) dirX += 1; + if (up) dirY -= 1; + if (down) dirY += 1; + + // Normalize diagonal movement + if (dirX != 0 && dirY != 0) { + // Calculate the normalized direction + float length = (float)Math.sqrt(dirX * dirX + dirY * dirY); + dirX /= length; + dirY /= length; } - if (up) { - velocity.y -= moveSpeed; - if (velocity.y < -maxSpeed) velocity.y = -maxSpeed; - if (spritesLoaded) currentAnim = upAnim; + + // Apply movement only if keys are pressed (no acceleration buildup) + if (dirX != 0) { + velocity.x = dirX * maxSpeed; } - if (down) { - velocity.y += moveSpeed; - if (velocity.y > maxSpeed) velocity.y = maxSpeed; - if (spritesLoaded) currentAnim = downAnim; + + if (dirY != 0) { + velocity.y = dirY * maxSpeed; } - // If no movement keys are pressed, use idle animation - if (!left && !right && !up && !down) { + // Set appropriate animation based on movement direction + if (velocity.x < 0 && Math.abs(velocity.x) > Math.abs(velocity.y)) { + if (spritesLoaded) currentAnim = leftAnim; + } else if (velocity.x > 0 && Math.abs(velocity.x) > Math.abs(velocity.y)) { + if (spritesLoaded) currentAnim = rightAnim; + } else if (velocity.y < 0 && Math.abs(velocity.y) > Math.abs(velocity.x)) { + if (spritesLoaded) currentAnim = upAnim; + } else if (velocity.y > 0 && Math.abs(velocity.y) > Math.abs(velocity.x)) { + if (spritesLoaded) currentAnim = downAnim; + } else if (velocity.x == 0 && velocity.y == 0) { if (spritesLoaded) currentAnim = idleAnim; } @@ -162,27 +173,6 @@ public void update() { currentAnim.reset(); } - // Apply friction - if (!left && !right) { - if (velocity.x > 0) { - velocity.x -= friction; - if (velocity.x < 0) velocity.x = 0; - } else if (velocity.x < 0) { - velocity.x += friction; - if (velocity.x > 0) velocity.x = 0; - } - } - - if (!up && !down) { - if (velocity.y > 0) { - velocity.y -= friction; - if (velocity.y < 0) velocity.y = 0; - } else if (velocity.y < 0) { - velocity.y += friction; - if (velocity.y > 0) velocity.y = 0; - } - } - // Calculate new position float newX = position.x + velocity.x; float newY = position.y + velocity.y; diff --git a/app/src/main/java/com/vincentramdhanie/twod/game/entity/Block.java b/app/src/main/java/com/niravramdhanie/twod/game/entity/Block.java similarity index 93% rename from app/src/main/java/com/vincentramdhanie/twod/game/entity/Block.java rename to app/src/main/java/com/niravramdhanie/twod/game/entity/Block.java index eb04e99..3cfb651 100644 --- a/app/src/main/java/com/vincentramdhanie/twod/game/entity/Block.java +++ b/app/src/main/java/com/niravramdhanie/twod/game/entity/Block.java @@ -1,9 +1,10 @@ -package com.vincentramdhanie.twod.game.entity; +package com.niravramdhanie.twod.game.entity; -import java.awt.Graphics2D; import java.awt.Color; +import java.awt.Graphics2D; import java.awt.image.BufferedImage; -import com.vincentramdhanie.twod.game.utils.ResourceLoader; + +import com.niravramdhanie.twod.game.utils.ResourceLoader; public class Block extends Entity { private BufferedImage blockImage; diff --git a/app/src/main/java/com/vincentramdhanie/twod/game/entity/Entity.java b/app/src/main/java/com/niravramdhanie/twod/game/entity/Entity.java similarity index 93% rename from app/src/main/java/com/vincentramdhanie/twod/game/entity/Entity.java rename to app/src/main/java/com/niravramdhanie/twod/game/entity/Entity.java index 1593a12..3e35514 100644 --- a/app/src/main/java/com/vincentramdhanie/twod/game/entity/Entity.java +++ b/app/src/main/java/com/niravramdhanie/twod/game/entity/Entity.java @@ -1,10 +1,10 @@ -package com.vincentramdhanie.twod.game.entity; - -import com.vincentramdhanie.twod.game.utils.Vector2D; +package com.niravramdhanie.twod.game.entity; import java.awt.Graphics2D; import java.awt.Rectangle; +import com.niravramdhanie.twod.game.utils.Vector2D; + public abstract class Entity { protected Vector2D position; protected Vector2D velocity; diff --git a/app/src/main/java/com/vincentramdhanie/twod/game/graphics/Animation.java b/app/src/main/java/com/niravramdhanie/twod/game/graphics/Animation.java similarity index 97% rename from app/src/main/java/com/vincentramdhanie/twod/game/graphics/Animation.java rename to app/src/main/java/com/niravramdhanie/twod/game/graphics/Animation.java index 5fada75..958d641 100644 --- a/app/src/main/java/com/vincentramdhanie/twod/game/graphics/Animation.java +++ b/app/src/main/java/com/niravramdhanie/twod/game/graphics/Animation.java @@ -1,4 +1,4 @@ -package com.vincentramdhanie.twod.game.graphics; +package com.niravramdhanie.twod.game.graphics; import java.awt.image.BufferedImage; import java.util.ArrayList; diff --git a/app/src/main/java/com/vincentramdhanie/twod/game/graphics/SpriteSheet.java b/app/src/main/java/com/niravramdhanie/twod/game/graphics/SpriteSheet.java similarity index 98% rename from app/src/main/java/com/vincentramdhanie/twod/game/graphics/SpriteSheet.java rename to app/src/main/java/com/niravramdhanie/twod/game/graphics/SpriteSheet.java index be5eb13..a7f9228 100644 --- a/app/src/main/java/com/vincentramdhanie/twod/game/graphics/SpriteSheet.java +++ b/app/src/main/java/com/niravramdhanie/twod/game/graphics/SpriteSheet.java @@ -1,4 +1,4 @@ -package com.vincentramdhanie.twod.game.graphics; +package com.niravramdhanie.twod.game.graphics; import java.awt.image.BufferedImage; diff --git a/app/src/main/java/com/vincentramdhanie/twod/game/input/KeyHandler.java b/app/src/main/java/com/niravramdhanie/twod/game/input/KeyHandler.java similarity index 90% rename from app/src/main/java/com/vincentramdhanie/twod/game/input/KeyHandler.java rename to app/src/main/java/com/niravramdhanie/twod/game/input/KeyHandler.java index 4922eb0..a68bd3f 100644 --- a/app/src/main/java/com/vincentramdhanie/twod/game/input/KeyHandler.java +++ b/app/src/main/java/com/niravramdhanie/twod/game/input/KeyHandler.java @@ -1,10 +1,10 @@ -package com.vincentramdhanie.twod.game.input; - -import com.vincentramdhanie.twod.game.core.GameStateManager; +package com.niravramdhanie.twod.game.input; import java.awt.event.KeyEvent; import java.awt.event.KeyListener; +import com.niravramdhanie.twod.game.core.GameStateManager; + public class KeyHandler implements KeyListener { private boolean[] keys; private GameStateManager gsm; diff --git a/app/src/main/java/com/vincentramdhanie/twod/game/input/MouseHandler.java b/app/src/main/java/com/niravramdhanie/twod/game/input/MouseHandler.java similarity index 94% rename from app/src/main/java/com/vincentramdhanie/twod/game/input/MouseHandler.java rename to app/src/main/java/com/niravramdhanie/twod/game/input/MouseHandler.java index 77545bc..c33f848 100644 --- a/app/src/main/java/com/vincentramdhanie/twod/game/input/MouseHandler.java +++ b/app/src/main/java/com/niravramdhanie/twod/game/input/MouseHandler.java @@ -1,11 +1,11 @@ -package com.vincentramdhanie.twod.game.input; - -import com.vincentramdhanie.twod.game.core.GameStateManager; +package com.niravramdhanie.twod.game.input; import java.awt.event.MouseEvent; import java.awt.event.MouseListener; import java.awt.event.MouseMotionListener; +import com.niravramdhanie.twod.game.core.GameStateManager; + public class MouseHandler implements MouseListener, MouseMotionListener { private int mouseX; private int mouseY; diff --git a/app/src/main/java/com/vincentramdhanie/twod/game/state/GameState.java b/app/src/main/java/com/niravramdhanie/twod/game/state/GameState.java similarity index 83% rename from app/src/main/java/com/vincentramdhanie/twod/game/state/GameState.java rename to app/src/main/java/com/niravramdhanie/twod/game/state/GameState.java index 20f52bd..bc7a632 100644 --- a/app/src/main/java/com/vincentramdhanie/twod/game/state/GameState.java +++ b/app/src/main/java/com/niravramdhanie/twod/game/state/GameState.java @@ -1,9 +1,9 @@ -package com.vincentramdhanie.twod.game.state; - -import com.vincentramdhanie.twod.game.core.GameStateManager; +package com.niravramdhanie.twod.game.state; import java.awt.Graphics2D; +import com.niravramdhanie.twod.game.core.GameStateManager; + public abstract class GameState { protected GameStateManager gsm; diff --git a/app/src/main/java/com/vincentramdhanie/twod/game/state/MenuState.java b/app/src/main/java/com/niravramdhanie/twod/game/state/MenuState.java similarity index 97% rename from app/src/main/java/com/vincentramdhanie/twod/game/state/MenuState.java rename to app/src/main/java/com/niravramdhanie/twod/game/state/MenuState.java index 74cf48b..aa0ff18 100644 --- a/app/src/main/java/com/vincentramdhanie/twod/game/state/MenuState.java +++ b/app/src/main/java/com/niravramdhanie/twod/game/state/MenuState.java @@ -1,8 +1,4 @@ -package com.vincentramdhanie.twod.game.state; - -import com.vincentramdhanie.twod.game.core.GameStateManager; -import com.vincentramdhanie.twod.game.ui.Button; -import com.vincentramdhanie.twod.game.utils.ResourceLoader; +package com.niravramdhanie.twod.game.state; import java.awt.Color; import java.awt.Font; @@ -11,6 +7,10 @@ import java.util.ArrayList; import java.util.List; +import com.niravramdhanie.twod.game.core.GameStateManager; +import com.niravramdhanie.twod.game.ui.Button; +import com.niravramdhanie.twod.game.utils.ResourceLoader; + public class MenuState extends GameState { private BufferedImage background; private List