Skip to content

Commit

Permalink
v0.2.0: Merge improvements made after trying it out with a gamejam ga…
Browse files Browse the repository at this point in the history
…me (Slime Bath). Remove some the dependency on AssetManager in GameEntity.
  • Loading branch information
jmsmith84 committed Apr 16, 2022
1 parent a005fed commit 3cf2ebb
Show file tree
Hide file tree
Showing 45 changed files with 714 additions and 230 deletions.
3 changes: 3 additions & 0 deletions build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -18,4 +18,7 @@ apply<KorgeGradlePlugin>()

korge {
id = "com.redhue.korgeboot"

targetJs()
targetJvm()
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,11 @@ import com.soywiz.klock.TimeSpan
import com.soywiz.klock.seconds
import com.soywiz.korge.component.FixedUpdateComponent
import containers.GameEntity
import containers.enemy.Enemy
import containers.enemy.SpriteEnemy
import containers.player.PLAYER_NAME

class ShootsBulletsAtPlayer(
override val view: Enemy,
override val view: SpriteEnemy,
private val bullet: GameEntity,
step: TimeSpan = 2.seconds,
maxAccumulated: Int = 10
Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
package components.collision

import com.soywiz.klock.DateTime
import com.soywiz.klock.TimeSpan
import com.soywiz.korge.component.UpdateComponent
import com.soywiz.korge.view.HitTestDirection
import containers.GameEntity
import program.LevelManager
import program.Log
import utility.*
import utility.getDeltaScale
import utility.viewHitTest
import kotlin.math.round

class MovesWithTilemapCollision(
Expand All @@ -16,24 +16,24 @@ class MovesWithTilemapCollision(
) : UpdateComponent {
override fun update(dt: TimeSpan) {
val delta = getDeltaScale(dt)
val mapView = levelManager.getCurrentMapView()
val mapView = levelManager.getMapView()

if (view.move.isMovingLeft() || view.move.isMovingRight()) {
if (view.isMovingLeft() || view.isMovingRight()) {
val oldX = view.x
view.x += round(view.move.x * delta)
view.x += round(view.move.x * delta * view.speedModifier)

if (mapView.viewHitTest(view, HitTestDirection.LEFT) !== null
|| mapView.viewHitTest(view, HitTestDirection.RIGHT) !== null
) {
Log().debug { "map hit X " + view.pos }

if (view.move.isMovingLeft()) {
if (view.isMovingLeft()) {
while (mapView.viewHitTest(view, HitTestDirection.LEFT) !== null
&& view.x < oldX
) {
view.x += 1.0
}
} else if (view.move.isMovingRight()) {
} else if (view.isMovingRight()) {
while (mapView.viewHitTest(view, HitTestDirection.RIGHT) !== null
&& view.x > oldX
) {
Expand All @@ -44,22 +44,22 @@ class MovesWithTilemapCollision(
}
}

if (view.move.isMovingUp() || view.move.isMovingDown()) {
if (view.isMovingUp() || view.isMovingDown()) {
val oldY = view.y
view.y += round(view.move.y * delta)
view.y += round(view.move.y * delta * view.speedModifier)

if (mapView.viewHitTest(view, HitTestDirection.UP) !== null
|| mapView.viewHitTest(view, HitTestDirection.DOWN) !== null
) {
Log().debug { "map hit Y " + view.pos }

if (view.move.isMovingUp()) {
if (view.isMovingUp()) {
while (mapView.viewHitTest(view, HitTestDirection.UP) !== null
&& view.y < oldY
) {
view.y += 1.0
}
} else if (view.move.isMovingDown()) {
} else if (view.isMovingDown()) {
while (mapView.viewHitTest(view, HitTestDirection.DOWN) !== null
&& view.y > oldY
) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,25 +1,21 @@
package components.collision

import com.soywiz.klock.DateTime
import com.soywiz.klock.TimeSpan
import com.soywiz.korge.component.UpdateComponent
import com.soywiz.korge.view.HitTestDirection
import containers.GameEntity
import program.LevelManager
import program.Log
import utility.*
import utility.getDeltaScale

class MovesWithoutTilemapCollision(
override val view: GameEntity
) : UpdateComponent {
override fun update(dt: TimeSpan) {
val delta = getDeltaScale(dt)

if (view.move.isMovingLeft() || view.move.isMovingRight()) {
view.x += view.move.x * delta
if (view.isMovingLeft() || view.isMovingRight()) {
view.x += view.move.x * delta * view.speedModifier
}
if (view.move.isMovingUp() || view.move.isMovingDown()) {
view.y += view.move.y * delta
if (view.isMovingUp() || view.isMovingDown()) {
view.y += view.move.y * delta * view.speedModifier
}
}
}
8 changes: 3 additions & 5 deletions src/commonMain/kotlin/components/input/HorizontalMoveInput.kt
Original file line number Diff line number Diff line change
@@ -1,13 +1,11 @@
package components.input

import com.soywiz.klock.TimeSpan
import com.soywiz.klock.milliseconds
import com.soywiz.korev.Key
import com.soywiz.korge.component.UpdateComponent
import com.soywiz.korge.component.UpdateComponentWithViews
import com.soywiz.korge.input.Input
import com.soywiz.korge.view.SpriteAnimation
import com.soywiz.korge.view.Views
import com.soywiz.korma.math.clamp
import containers.SpriteEntity

class HorizontalMoveInput(
Expand All @@ -18,10 +16,10 @@ class HorizontalMoveInput(
override fun update(views: Views, dt: TimeSpan) {
if (views.input.keys[Key.LEFT]) {
view.move.x -= 1.0
if (leftAnimation !== null) view.getSprite().playAnimationLooped(leftAnimation)
if (leftAnimation !== null) view.getSprite().playAnimationLooped(leftAnimation, spriteDisplayTime = 130.milliseconds)
} else if (views.input.keys[Key.RIGHT]) {
view.move.x += 1.0
if (rightAnimation !== null) view.getSprite().playAnimationLooped(rightAnimation)
if (rightAnimation !== null) view.getSprite().playAnimationLooped(rightAnimation, spriteDisplayTime = 130.milliseconds)
} else {
view.move.x = 0.0
}
Expand Down
5 changes: 3 additions & 2 deletions src/commonMain/kotlin/components/input/VerticalMoveInput.kt
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package components.input

import com.soywiz.klock.TimeSpan
import com.soywiz.klock.milliseconds
import com.soywiz.korev.Key
import com.soywiz.korge.component.UpdateComponentWithViews
import com.soywiz.korge.view.SpriteAnimation
Expand All @@ -15,10 +16,10 @@ class VerticalMoveInput(
override fun update(views: Views, dt: TimeSpan) {
if (views.input.keys[Key.UP]) {
view.move.y -= 1.0
if (upAnimation !== null) view.getSprite().playAnimationLooped(upAnimation)
if (upAnimation !== null) view.getSprite().playAnimationLooped(upAnimation, spriteDisplayTime = 130.milliseconds)
} else if (views.input.keys[Key.DOWN]) {
view.move.y += 1.0
if (downAnimation !== null) view.getSprite().playAnimationLooped(downAnimation)
if (downAnimation !== null) view.getSprite().playAnimationLooped(downAnimation, spriteDisplayTime = 130.milliseconds)
} else {
view.move.y = 0.0
}
Expand Down
10 changes: 8 additions & 2 deletions src/commonMain/kotlin/components/movement/ClampMovement.kt
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,18 @@ import com.soywiz.korma.geom.XY
import com.soywiz.korma.math.clamp
import containers.GameEntity

@Suppress("MemberVisibilityCanBePrivate")
class ClampMovement(
override val view: GameEntity,
val maxSpeedXY: XY
) : UpdateComponent {
override fun update(dt: TimeSpan) {
view.move.x = view.move.x.clamp(-maxSpeedXY.x, maxSpeedXY.x)
view.move.y = view.move.y.clamp(-maxSpeedXY.y, maxSpeedXY.y)
if (view.canMove) {
view.move.x = view.move.x.clamp(-maxSpeedXY.x, maxSpeedXY.x)
view.move.y = view.move.y.clamp(-maxSpeedXY.y, maxSpeedXY.y)
} else {
//Log().warn { "move input disabled" }
view.move.setTo(0, 0)
}
}
}
32 changes: 32 additions & 0 deletions src/commonMain/kotlin/components/movement/HasFacing.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package components.movement

import com.soywiz.klock.TimeSpan
import com.soywiz.korge.component.UpdateComponent
import containers.GameEntity

@Suppress("MemberVisibilityCanBePrivate")
class HasFacing(
override val view: GameEntity,
initialDirection: MoveDirection = MoveDirection.DOWN
) : UpdateComponent {
private var facing: MoveDirection = initialDirection

override fun update(dt: TimeSpan) {
val directions = view.getMoveDirections()

// Priority order: l+r > u+d
if (directions.contains(MoveDirection.RIGHT)) {
facing = MoveDirection.RIGHT
} else if (directions.contains(MoveDirection.LEFT)) {
facing = MoveDirection.LEFT
} else if (directions.contains(MoveDirection.UP)) {
facing = MoveDirection.UP
} else if (directions.contains(MoveDirection.DOWN)) {
facing = MoveDirection.DOWN
}
}

fun getFacing(): MoveDirection {
return facing
}
}
2 changes: 1 addition & 1 deletion src/commonMain/kotlin/components/movement/MoveDirection.kt
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
package components.movement

enum class MoveDirection {
UP, DOWN, LEFT, RIGHT
UP, DOWN, LEFT, RIGHT, NONE
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@ package components.movement
import com.soywiz.klock.TimeSpan
import com.soywiz.korge.component.UpdateComponent
import com.soywiz.korge.view.View
import com.soywiz.korma.geom.*
import com.soywiz.korma.geom.XY
import com.soywiz.korma.geom.add
import com.soywiz.korma.geom.times
import utility.getDeltaScale

class StraightMovement(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,7 @@ package components.movement

import com.soywiz.klock.TimeSpan
import com.soywiz.korge.component.UpdateComponent
import com.soywiz.korge.view.View
import containers.GameEntity
import utility.getDeltaScale
import kotlin.math.sin

class VerticalSineMovement(
Expand Down
68 changes: 63 additions & 5 deletions src/commonMain/kotlin/containers/GameEntity.kt
Original file line number Diff line number Diff line change
@@ -1,23 +1,40 @@
package containers

import com.soywiz.korge.view.BaseImage
import com.soywiz.korge.view.Container
import com.soywiz.korge.view.HitTestDirection
import com.soywiz.korge.view.RectBase
import com.soywiz.korge.view.addTo
import com.soywiz.korma.geom.Point
import com.soywiz.korma.geom.Rectangle
import program.IAssetManager
import components.movement.MoveDirection
import program.LevelManager
import program.SoundManager
import utility.rectHitTest

open class GameEntity(
protected val assets: IAssetManager,
protected var image: RectBase,
protected val soundManager: SoundManager,
protected val levelManager: LevelManager,
protected var hp: UInt = 1u
) : Container() {
val move = Point(0, 0)
lateinit var image: BaseImage
var canMove = true
var speedModifier: Double = 1.0

init {
image.addTo(this)
}

fun getHP(): UInt {
return hp
}

open fun damage(amount: UInt = 1u) {
if (amount > hp) hp = 0u else hp -= amount
if (hp == 0u) kill()
}

open fun kill() {}

fun getCurrentBounds(): Rectangle {
return getGlobalBounds()
Expand All @@ -26,6 +43,47 @@ open class GameEntity(
fun isTouchingGround(): Boolean {
val clone = getCurrentBounds().clone()
clone.y++
return levelManager.getCurrentMapView().rectHitTest(clone, HitTestDirection.DOWN) !== null
return levelManager.getMapView().rectHitTest(clone, HitTestDirection.DOWN) !== null
}

fun isMovingLeft(): Boolean {
return move.x < 0.0
}

fun isMovingRight(): Boolean {
return move.x > 0.0
}

fun isMovingUp(): Boolean {
return move.y < 0.0
}

fun isMovingDown(): Boolean {
return move.y > 0.0
}

fun isMoving(): Boolean {
return !getMoveDirections().contains(MoveDirection.NONE)
}

fun getMoveDirections(): Set<MoveDirection> {
val set = mutableSetOf<MoveDirection>()

if (!isMovingLeft() && !isMovingRight() && !isMovingUp() && !isMovingDown()) {
set.add(MoveDirection.NONE)
return set
}

if (isMovingLeft()) {
set.add(MoveDirection.LEFT)
} else if (isMovingRight()) {
set.add(MoveDirection.RIGHT)
}
if (isMovingDown()) {
set.add(MoveDirection.DOWN)
} else if (isMovingUp()) {
set.add(MoveDirection.UP)
}
return set
}
}
13 changes: 5 additions & 8 deletions src/commonMain/kotlin/containers/SpriteEntity.kt
Original file line number Diff line number Diff line change
@@ -1,20 +1,17 @@
package containers

import com.soywiz.korge.view.Sprite
import com.soywiz.korge.view.addTo
import com.soywiz.korge.view.anchor
import program.IAssetManager
import program.LevelManager
import program.Settings
import program.SoundManager

open class SpriteEntity(sprite: Sprite, assets: IAssetManager, soundManager: SoundManager, levelManager: LevelManager)
: GameEntity(assets, soundManager, levelManager) {
open class SpriteEntity(sprite: Sprite, soundManager: SoundManager, levelManager: LevelManager)
: GameEntity(sprite, soundManager, levelManager) {

init {
image = sprite
.anchor(0,0)
.addTo(this)
image.smoothing = false
image.anchor(0,0)
image.smoothing = Settings.spriteSmoothing
}

fun getSprite(): Sprite {
Expand Down
Loading

0 comments on commit 3cf2ebb

Please sign in to comment.