Commit 9811b299 authored by Daniel Drießen's avatar Daniel Drießen

Implemented Rotation

parent f5866cd3
......@@ -53,8 +53,18 @@ class Player(pm: PathManager) : Sprite(pm) {
fun isOnPath(): Boolean {
val unified = Path()
for (path in pm.getPaths()) unified.addPath(path)
unified.transform(Matrix().apply {
setRotate(
-pm.degreesRotated + 0f,
pm.game.size.x / 2,
pm.game.size.y / 2
)
})
if (pm.game.options.difficulty.diffRes < Constants.Difficulty.HARD.diffRes)
shields.removeAll { !(it.contains(unified)) }
return contains(unified)
}
......@@ -73,11 +83,14 @@ class Player(pm: PathManager) : Sprite(pm) {
var rs: Shields? = null
for (sh in shields)
for (e in pm.getEnemies())
for (e in pm.getEnemies()) {
e.detransform()
if (sh.intersects(e)) {
pm.removeEnemy(e)
rs = sh
}
e.transform()
}
shields.remove(rs)
}
......
package io.ender.pathchaser
import android.graphics.Canvas
import android.graphics.Paint
import android.graphics.Path
import android.graphics.Region
import android.graphics.*
import io.ender.pathchaser.paths.PathManager
import java.io.Serializable
......@@ -20,4 +17,24 @@ abstract class Sprite(val pm: PathManager) : Path(), Serializable {
open fun move(x: Float, y: Float) {
offset(x, y)
}
fun transform() {
transform(Matrix().apply {
setRotate(
pm.degreesRotated,
pm.game.size.x / 2,
pm.game.size.y / 2
)
})
}
fun detransform() {
transform(Matrix().apply {
setRotate(
-pm.degreesRotated,
pm.game.size.x / 2,
pm.game.size.y / 2
)
})
}
}
......@@ -13,7 +13,15 @@ class CirclePath(pm: PathManager, start: PointF, r: Float, enemyNumber: Int = 0)
for (i in 1..enemyNumber) {
enemies.add(
Enemy(PointF(start.x, start.y + r / 2), pm)
.apply { transform(Matrix().apply { setRotate((i*360/enemyNumber).toFloat(), start.x, start.y) }) }
.apply {
transform(Matrix().apply {
setRotate(
(i * 360 / enemyNumber).toFloat(),
start.x,
start.y
)
})
}
)
}
pm.addRandomCollectible(start, this)
......@@ -28,9 +36,11 @@ class CirclePath(pm: PathManager, start: PointF, r: Float, enemyNumber: Int = 0)
super.update()
for (e in enemies) {
e.transform(Matrix().apply { setRotate(pm.game.speed, start.x, start.y) })
e.detransform()
if (e.intersects(pm.player)) {
pm.player.lives--
}
e.transform()
}
}
......
package io.ender.pathchaser.paths
import android.graphics.Canvas
import android.graphics.Matrix
import android.graphics.PointF
import android.graphics.Region
import android.util.Log
import io.ender.pathchaser.util.Constants
import io.ender.pathchaser.Game
import io.ender.pathchaser.Player
import io.ender.pathchaser.collectibles.Coin
import io.ender.pathchaser.collectibles.Shields
import io.ender.pathchaser.collectibles.SpeedDown
import io.ender.pathchaser.collectibles.SpeedUp
import io.ender.pathchaser.collectibles.*
import io.ender.pathchaser.enemys.Enemy
import java.io.Serializable
......@@ -18,8 +16,8 @@ class PathManager(val game: Game) : Serializable {
private val paths: MutableList<Pathway> = mutableListOf()
fun getPaths(): List<Pathway> = paths
lateinit var player: Player
var degreesRotated = 0
var degreesToRotate = 0
var degreesRotated = 0f
var degreesToRotate = 0f
init {
// start path wrapping the player
......@@ -29,12 +27,16 @@ class PathManager(val game: Game) : Serializable {
}
fun draw(canvas: Canvas) {
canvas.rotate(-degreesRotated, game.size.x / 2, game.size.y / 2)
for (path in paths)
if (path is RectPath)
path.draw(canvas, path.getPaint())
for (path in paths)
if (path !is RectPath)
path.draw(canvas, path.getPaint())
canvas.rotate(degreesRotated, game.size.x / 2, game.size.y / 2)
}
fun update() {
......@@ -42,6 +44,7 @@ class PathManager(val game: Game) : Serializable {
Log.e("PM", "empty path list!")
return
}
for (path in paths) {
path.update()
path.move(0f, game.speed)
......@@ -51,16 +54,30 @@ class PathManager(val game: Game) : Serializable {
if (!paths.first().isOnSurface(Region(0, 0, game.size.x.toInt(), game.size.y.toInt()))) {
paths.removeAt(0)
}
if(paths.last().isOnSurface(Region(0, 0, game.size.x.toInt(), game.size.y.toInt())))
if (paths.last().isOnSurface(Region(0, 0, game.size.x.toInt(), game.size.y.toInt())))
extendPath()
if (degreesRotated - degreesToRotate < 0) {
degreesRotated += 0.5f
} else {
if (degreesRotated - degreesToRotate > 0)
degreesRotated += -0.5f
}
}
private fun extendPath() {
val upper = paths.last().end
paths.add(
when (if (paths.last() is RectPath) (0..3).random() else 0) {
0 -> RectPath(this, Constants.playerRadius*4.2f, 400f, PointF(upper.x, upper.y - 75))
1 -> ArcPath(this, Constants.playerRadius*4.2f, 600f, PointF(upper.x, upper.y - 200), (0..1).random() == 0)
0 -> RectPath(this, Constants.playerRadius * 4.2f, 400f, PointF(upper.x, upper.y - 75))
1 -> ArcPath(
this,
Constants.playerRadius * 4.2f,
600f,
PointF(upper.x, upper.y - 200),
(0..1).random() == 0
)
2 -> CirclePath(this, PointF(upper.x, upper.y - 200), 300f, (0..4).random())
3 -> ForkPath(this, PointF(upper.x, upper.y - 250), 800, 200, 200)
else -> throw IllegalStateException()
......@@ -69,7 +86,7 @@ class PathManager(val game: Game) : Serializable {
}
fun getEnemies(): List<Enemy> {
var enemies: MutableList<Enemy> = mutableListOf()
val enemies: MutableList<Enemy> = mutableListOf()
for (path in paths)
enemies.addAll(path.enemies)
return enemies
......@@ -86,6 +103,7 @@ class PathManager(val game: Game) : Serializable {
in 1..3 -> SpeedUp(this, p)
in 5..6 -> SpeedDown(this, p)
in 10..12 -> Shields(this, p)
in 15..18 -> Rotation(this, p)
else -> Coin(this, p)
}
)
......
......@@ -18,17 +18,6 @@ abstract class Pathway(pm: PathManager, val start: PointF = PointF(), val end: P
override fun getPaint(): Paint = Paint().apply { color = Color.WHITE }
fun applyMatrix(matrix: Matrix) {
val trans = floatArrayOf(end.x, end.y)
matrix.mapPoints(trans)
end.x = trans[0]
end.y = trans[1]
transform(matrix)
}
fun rotate(deg: Float, p: PointF = PointF(-1f, -1f)) =
applyMatrix(Matrix().apply { if (p.x == -1f) setRotate(deg) else setRotate(deg, p.x, p.y) })
override fun move(x: Float, y: Float) {
super.move(x, y)
start.x += x
......@@ -54,8 +43,11 @@ abstract class Pathway(pm: PathManager, val start: PointF = PointF(), val end: P
var collected: Collectible? = null
if (showCollectibles)
for (coll in collectibles)
if (p.intersects(coll))
if (p.intersects(coll.apply { detransform() }))
collected = coll
else
coll.transform()
collectibles.remove(collected)
return collected
}
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment