Skip to content

A 2D Game Engine with a component-based architecture style, dynamic audio scaling, separating axis theorem collision detection.

Notifications You must be signed in to change notification settings

Kali-Zoidberg/Java-Engine

Repository files navigation

JEngine

JEngine spawned after wanting to rewrite a version of Snakey I created in high school. A few days after optimization and reorganization, I decided to create a full fledged 2D game engine. There are still a few bugs to iron out, and areas I need to address but regardless, it is possible for anyone to make a video game utilizing this engine.
The following write up will be updated over the course of the next two months.

How to Create a Scene

1] Create a static Render Object

public static int frameRate = 60;
public static int screenWidth = 500;
public static int screenHeight = 500;
public static Render renderObj = new Render("Window Title", frameRate, screenWidth, screenHeight);

public static void main(String[] args)
{
    initializeScene();
}

public static initializeScene()
{
    //renderObj.setBackground(Image img); // Set the background using an image
    renderObj.setBackground(Color.RED);
    
    /*
    Your scene intialization code here.
    */
}


The following code is to be placed in the initialization function.
2] Create an actor

Actor squareActor = new Actor(x_pos, y_pos, width, height, "Actor Name");


3] Assign a collision shape to the actor's rigid body and set the actor's visibility and collision to true.

squareActor.rigidbody.collisionShape = new Rectangle(x_pos, y_pos, width, height);
squareActor.setCollision(true);
squareActor.setVisible(true);


4] Set the sprite for the Actor

squareActor.setSprite(new Sprite(Image sprite_image, width, height);

OR Set the color for the Actor.

squareActor.setColor(Color.RED);


5]Create the update object, set the update method and start the scene.

MyUpdate updateMethod = new MyUpdate();
renderObj.setUpdateMethod(updateMethod);
renderObj.start();


The Initialization code all together

public static int frameRate = 60;
public static int screenWidth = 500;
public static int screenHeight = 500;
public static Render renderObj = new Render("Window Title", frameRate, screenWidth, screenHeight);

public static void main(String[] args)
{
    initializeScene();
}

public static void initializeScene()
{
    //renderObj.setBackground(Image img); // Set the background using an image
    renderObj.setBackground(Color.WHITE);
    
    int x_pos = 50;
    int y_pos = 100;
    int width = 50, height = 50;
    
    Actor squareActor = new Actor(x_pos, y_pos, width, height, "Actor Name"); //Create the actor
    squareActor.rigidbody.collisionShape = new Rectangle(x_pos, y_pos, width, height); // Set the collision of the actor.
    //squareActor.setSprite(new Sprite(new Image("Filepath"), width, height); //Give the actor a sprite and set the image of the sprite.
    squareActor.setColor(Color.RED); //Set the color of the actor
    squareActor.setCollision(true);
    squareActor.setVisible(true);
    
    MyUpdate updateMethod = new MyUpdate();
renderObj.setUpdateMethod(updateMethod);
    renderObj.start();
}

6] Edit/Extend Runnable to create an update method
The update method will be called each game tick. This method, in particular changes the actor's colors randomly.
static class MyUpdate implements Runnable 	
{
	
	public void run()
	{ 
		
		for (GameObject goj: GameWorld.game_obj_table.values())
		{
			Actor someActor= (Actor) goj;
			changeColor(someActor);
		}
	
	}
	public void changeColor(Actor actorA)
	{
		Random rand = new Random(System.nanoTime());
		rand.setSeed(System.nanoTime());
		//Calculate random r,g,b colors
		float r = rand.nextFloat();
		float g = rand.nextFloat();
		float b = rand.nextFloat();
		
		Color randomColor = new Color(r, g, b); //Initalize the new color
		actorA.setColor(randomColor); //Set the color.
	}
	
}



Your game should look like this after completion.

Component Based Architecture:
The base class for all parent objects is a game object. Parents are parameterized as mentors.
The base class for child objects is component. Children are parameterized as component.
Below is a code snippet for the actual implementation of the component based architecture.


This architecture allows for both the parent (mentor) and child (component) to have direct access to one another and invoke their appropriate functions.

Dynamic Audio Scaling
Dynamic Audio Scaling is a feature in this engine to allow creators to tag/categorize game sounds, music, etc. as lines or categories.
Through these categories, creators may create a new line or category on the AudioMaster by calling the static function:

AudioLine audioLine = new AudioLine("Your line name");

The following code shows how to set the volume of an audio line and how to adjust all game sounds.

   audioLine.setVolLinear(float fraction); // Scales the volume based on a fraction or percentage.
   audioLine.setVolDB(float db); //Sets the volume of the line to a specified DB.
   audioLine.scaleVol(float fraction); //Scales the volume fractionally. 

To scale down all volumes simply call the following function.

    AudioMaster.scaleVolume(float volFrac); //Scales all audio lines (and subsequent game sounds) based on the given fraction/percentage.

About

A 2D Game Engine with a component-based architecture style, dynamic audio scaling, separating axis theorem collision detection.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages