Skip to content

Commit

Permalink
Merge pull request #8 from JohnSundell/actor-rect
Browse files Browse the repository at this point in the history
Actor: Compute rect manually
  • Loading branch information
JohnSundell authored Oct 22, 2017
2 parents 1edfcbe + 98f61d4 commit 53660bc
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 3 deletions.
15 changes: 12 additions & 3 deletions Sources/Core/API/Actor.swift
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ public final class Actor: InstanceHashable, ActionPerformer, Activatable, Movabl
/// The size of the actor (centered on its position).
public var size = Size() { didSet { sizeDidChange(from: oldValue) } }
/// The rectangle the actor currently occupies within its scene.
public var rect: Rect { return layer.frame }
public private(set) var rect = Rect() { didSet { rectDidChange() } }
/// The rotation of the actor along the z axis.
public var rotation = Metric() { didSet { layer.rotation = rotation } }
/// The scale of the actor. Does not affect its size, rect or collision detection.
Expand Down Expand Up @@ -158,9 +158,10 @@ public final class Actor: InstanceHashable, ActionPerformer, Activatable, Movabl
}

layer.position = position
updateRect()

events.moved.trigger()
events.rectChanged.trigger()
rectDidChange()
}

private func sizeDidChange(from oldValue: Size) {
Expand All @@ -169,9 +170,10 @@ public final class Actor: InstanceHashable, ActionPerformer, Activatable, Movabl
}

layer.bounds.size = size
updateRect()

events.resized.trigger()
events.rectChanged.trigger()
rectDidChange()
}

private func rectDidChange() {
Expand Down Expand Up @@ -227,6 +229,13 @@ public final class Actor: InstanceHashable, ActionPerformer, Activatable, Movabl
}
}

private func updateRect() {
var newRect = Rect(origin: position, size: size)
newRect.origin.x -= size.width / 2
newRect.origin.y -= size.height / 2
rect = newRect
}

private func renderFirstAnimationFrameIfNeeded() {
guard let animation = animation else {
return
Expand Down
10 changes: 10 additions & 0 deletions Tests/ImagineEngineTests/ActorTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,16 @@ final class ActorTests: XCTestCase {

// MARK: - Tests

func testRect() {
XCTAssertEqual(actor.rect, .zero)

actor.position = Point(x: 150, y: 200)
XCTAssertEqual(actor.rect, Rect(x: 150, y: 200, width: 0, height: 0))

actor.size = Size(width: 100, height: 300)
XCTAssertEqual(actor.rect, Rect(x: 100, y: 50, width: 100, height: 300))
}

func testAnimationAutoResizingActor() {
let imageSizes = [
Size(width: 200, height: 150),
Expand Down

0 comments on commit 53660bc

Please # to comment.