Skip to content

Commit

Permalink
add README
Browse files Browse the repository at this point in the history
  • Loading branch information
vgupta98 committed Jul 25, 2024
1 parent 4a6c7fd commit 090d2c0
Showing 1 changed file with 100 additions and 0 deletions.
100 changes: 100 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
# Compose Game Engine

A simple 2D game engine built with Jetpack Compose. This library provides basic game engine functionality, including game object management, collision detection, and rendering. It utilizes compose Animatable and Canvas APIs. However, it is good for small games only.

## Features

- Add and manage game objects (round objects and boundaries for now)
- Start, pause, and stop the game loop
- Collision detection with customizable restitution
- Listener support for game events
- Rendering of game objects using Jetpack Compose

Feel free to experiment and add your own features.

## Installation

Add the dependency to your `build.gradle.kts` file:

```kotlin
dependencies {
implementation("io.github.vgupta98:compose_game:1.0.0")
}
```

## Usage

1. Create an instance of the GameEngine:
```kotlin
val gameEngine = GameFactory.getInstance()
```
2. Add round objects and boundaries to the game engine:
```kotlin
val roundedObject = RoundedObject(
id = 1,
initialPosition = Vector2D(100f, 100f),
radius = 20f,
mass = 1f,
initialVelocity = Vector2D(1f, 1f),
restitution = 0.8f
)

val boundary = Boundary(
id = 2,
startPosition = Vector2D(0f, 0f),
endPosition = Vector2D(300f, 0f),
restitution = 1f
)

gameEngine.addGameObject(roundedObject)
gameEngine.addGameObject(boundary)
```
3. Start the game loop using a CoroutineScope:
```kotlin
val scope = CoroutineScope(Dispatchers.Main)
gameEngine.startGameLoop(scope)
```
4. Stop the game loop:
```kotlin
gameEngine.stopGameLoop(scope)
```
5. Use the `GameBoard` composable to render the game objects:
```kotlin
@Composable
fun MyGameScreen() {
val gameResources = listOf(
RoundedObjectResource(
id = 1,
painter = rememberVectorPainter(image = Icons.Default.Circle)
),
BoundaryResource(
id = 2,
color = Color.Black,
thicknessInPx = 2f
)
)

GameBoard(
modifier = Modifier.fillMaxSize(),
gameEngine = gameEngine,
gameResources = gameResources,
onDrawAbove = { /* Custom drawing above the game objects */ },
onDrawBehind = { /* Custom drawing behind the game objects */ }
)
}
```
6. Listening to collisions is also easy. Implement the GameListener interface to listen for collisions:
```kotlin
class MyGameListener : GameListener {
override fun onCollision(objectId1: Int, objectId2: Int) {
println("Collision between object $objectId1 and object $objectId2")
}
}

val listener = MyGameListener()
gameEngine.addListener(listener)
```

## Contribution

Bug reports and pull requests are welcome. There is a lot room for optimizations and features!

0 comments on commit 090d2c0

Please sign in to comment.