Skip to content

Commit

Permalink
minor improvements
Browse files Browse the repository at this point in the history
  • Loading branch information
mprevel authored and davesmith00000 committed Dec 2, 2023
1 parent 89e1068 commit 935923b
Show file tree
Hide file tree
Showing 13 changed files with 32 additions and 34 deletions.
2 changes: 1 addition & 1 deletion indigo/indigo/src/main/scala/indigo/physics/World.scala
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ final case class World[A](colliders: Batch[Collider[A]], forces: Batch[Vector2],
def removeByTag(tag: A)(using CanEqual[A, A]): World[A] =
this.copy(colliders = colliders.filterNot(_.tag == tag))

def modifyByTag(tag: A)(f: Collider[A] => Collider[A])(using CanEqual[A, A]) =
def modifyByTag(tag: A)(f: Collider[A] => Collider[A])(using CanEqual[A, A]): World[A] =
this.copy(colliders = colliders.map(c => if c.tag == tag then f(c) else c))

def findFirstAt(position: Vertex): Option[Collider[A]] =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ final class GlobalEventStream(
}
}

if errors.length > 0 then errors foreach (e => eventQueue.enqueue(e))
if errors.nonEmpty then errors foreach (e => eventQueue.enqueue(e))
else
eventQueue.enqueue(StorageEvent.KeysFound(keys.flatMap {
_ match {
Expand Down
5 changes: 3 additions & 2 deletions indigo/indigo/src/main/scala/indigo/shared/QuickCache.scala
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,9 @@ final class QuickCache[A](private val cache: scalajs.js.Dictionary[A]):
cache.get(key.toString)

def add(key: CacheKey, value: => A): A = {
cache.update(key.toString, value)
value
val v = value
cache.update(key.toString, v)
v
}

def fetchOrAdd(key: CacheKey, disabled: Boolean, value: => A): A =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,7 @@ object Timeline:
* `Seconds`
*/
def duration: Seconds =
tl.map(_.end).sortWith(_ > _).headOption.getOrElse(Seconds.zero)
tl.maxByOption(_.end.toDouble).fold(Seconds.zero)(_.end)

/** Alias for duration, returns the total time duration of the animation in seconds, including any initial delays.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,12 @@ sealed trait Batch[+A]:
def map[B](f: A => B): Batch[B] =
Batch.Wrapped(_jsArray.map(f))

def maxBy[B](f: A => B)(using ord: Ordering[B]): A =
_jsArray.maxBy(f)(ord)

def maxByOption[B](f: A => B)(using ord: Ordering[B]): Option[A] =
Option.when(_jsArray.nonEmpty)(_jsArray.maxBy(f)(ord))

/** Converts the batch into a String`
* @return
* `String`
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,25 +33,15 @@ final case class EventFilters(
object EventFilters {

private def fromAccessControl(ac: AccessControl): GlobalEvent => Option[GlobalEvent] = {
case e: AssetEvent if ac.allowAssetEvents => Some(e)
case _: AssetEvent => None
case FrameTick if ac.allowFrameTick => Some(FrameTick)
case FrameTick => None
case e: KeyboardEvent if ac.allowKeyboardEvents => Some(e)
case _: KeyboardEvent => None
case e: MouseEvent if ac.allowMouseEvents => Some(e)
case _: MouseEvent => None
case e: NetworkReceiveEvent if ac.allowNetworkEvents => Some(e)
case _: NetworkReceiveEvent => None
case e: StorageEvent if ac.allowStorageEvents => Some(e)
case _: StorageEvent => None
case e: SubSystemEvent if ac.allowSubSystemEvents => Some(e)
case _: SubSystemEvent => None
case e: ViewEvent if ac.allowViewEvents => Some(e)
case _: ViewEvent => None
case e if ac.allowCustomEvents => Some(e)
case _ if ac.allowCustomEvents => None
case _ => None
case FrameTick => Option.when(ac.allowFrameTick)(FrameTick)
case e: KeyboardEvent => Option.when(ac.allowKeyboardEvents)(e)
case e: MouseEvent => Option.when(ac.allowMouseEvents)(e)
case e: NetworkReceiveEvent => Option.when(ac.allowNetworkEvents)(e)
case e: StorageEvent => Option.when(ac.allowStorageEvents)(e)
case e: SubSystemEvent => Option.when(ac.allowSubSystemEvents)(e)
case e: ViewEvent => Option.when(ac.allowViewEvents)(e)
case e: AssetEvent => Option.when(ac.allowAssetEvents)(e)
case e => Option.when(ac.allowCustomEvents)(e)
}

/** Access controlled event filters are a convienient way to have explicit control which events arrive at which
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,7 @@ trait MouseOrPointerEvent:

/** Indicates whether buttons are in active state
*/
def isActive: Boolean = buttons.isEmpty == false
def isActive: Boolean = !buttons.isEmpty

/** Whether the `alt` key was pressed when the event was fired
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,8 @@ final case class InputMapping[A](oneOf: List[(Combo, A)]) {
case MouseInput.MouseAt(pt) => mouse.position == pt
case MouseInput.MouseButtonUp(button) => mouse.released(button)
case MouseInput.MouseButtonDown(button) => mouse.pressed(button)
case MouseInput.MouseWheelDown => mouse.scrolled.exists(_ == MouseWheel.ScrollDown)
case MouseInput.MouseWheelUp => mouse.scrolled.exists(_ == MouseWheel.ScrollUp)
case MouseInput.MouseWheelDown => mouse.scrolled.contains(MouseWheel.ScrollDown)
case MouseInput.MouseWheelUp => mouse.scrolled.contains(MouseWheel.ScrollUp)
} &&
c._1.keyInputs.forall(k => keyboard.keysDown.contains(k)) &&
c._1.gamepadInputs.forall {
Expand Down
4 changes: 2 additions & 2 deletions indigo/indigo/src/main/scala/indigo/shared/input/Mouse.scala
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,8 @@ final class Mouse(
case _: MouseEvent.Click => true
case _ => false
}
lazy val mousePressed = pressed(MouseButton.LeftMouseButton)
lazy val mouseReleased = released(MouseButton.LeftMouseButton)
lazy val mousePressed: Boolean = pressed(MouseButton.LeftMouseButton)
lazy val mouseReleased: Boolean = released(MouseButton.LeftMouseButton)

def pressed(button: MouseButton): Boolean =
mouseEvents.exists {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ object Pointers:
val default: Pointers =
Pointers(Batch.empty, Point.zero)

def calculateNext(previous: Pointers, events: Batch[PointerEvent]) =
def calculateNext(previous: Pointers, events: Batch[PointerEvent]): Pointers =
Pointers(
pointerEvents = events,
position = lastPointerPosition(previous.position, events)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -939,6 +939,7 @@ object DisplayObjectConversions {
cacheKey: String,
disableCache: Boolean
)(using QuickCache[scalajs.js.Array[Float]]): scalajs.js.Array[Float] = {
@tailrec
def rec(
remaining: scalajs.js.Array[ShaderPrimitive],
current: scalajs.js.Array[Float],
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,8 @@ object Camera:
* while controlling the position, zoom and rotation.
*/
final case class Fixed(position: Point, zoom: Zoom, rotation: Radians) extends Camera:
def topLeft(viewport: GameViewport) = position
val isLookAt: Boolean = false
def topLeft(viewport: GameViewport): Point = position
val isLookAt: Boolean = false

def withX(newX: Int): Fixed =
this.copy(position = position.withX(newX))
Expand Down Expand Up @@ -68,7 +68,7 @@ object Camera:
final case class LookAt(target: Point, zoom: Zoom, rotation: Radians) extends Camera:
val isLookAt: Boolean = true
val position: Point = target
def topLeft(viewport: GameViewport) =
def topLeft(viewport: GameViewport): Point =
target - Point(viewport.width / 2, viewport.height / 2) / zoom.toDouble.toInt

def withTarget(newTarget: Point): LookAt =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ final case class SceneAudio(

object SceneAudio:

val Mute = SceneAudio(None, None, None)
val Mute: SceneAudio = SceneAudio(None, None, None)

def apply(sourceA: SceneAudioSource): SceneAudio =
SceneAudio(Some(sourceA), None, None)
Expand Down

0 comments on commit 935923b

Please sign in to comment.