-
-
Notifications
You must be signed in to change notification settings - Fork 873
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat!: Move Forge2DGame
to use CameraComponent
#2728
Changes from all commits
e43cb21
6520253
56e5374
6e6c3cd
4d9c78a
5e76cc5
1a1cf5f
fccc65e
726a278
918638e
53dd6e2
84fe542
0b63e5a
2070a7e
b220b71
38b1944
2409bd5
2818512
c23790f
d5a74f3
0a68dc1
7a957ee
0da0eb9
112e1a6
eada637
7783ab0
248efea
475129f
4c26d64
814d980
299ad75
23cf212
e9de3cd
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -47,7 +47,6 @@ class PadRacingGame extends Forge2DGame with KeyboardEvents { | |
static final Vector2 trackSize = Vector2.all(500); | ||
static const double playZoom = 8.0; | ||
static const int numberOfLaps = 3; | ||
late final World cameraWorld; | ||
late CameraComponent startCamera; | ||
late List<Map<LogicalKeyboardKey, LogicalKeyboardKey>> activeKeyMaps; | ||
late List<Set<LogicalKeyboardKey>> pressedKeySets; | ||
|
@@ -58,13 +57,13 @@ class PadRacingGame extends Forge2DGame with KeyboardEvents { | |
|
||
@override | ||
Future<void> onLoad() async { | ||
super.onLoad(); | ||
cameraComponent.removeFromParent(); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I imagine this is because it gets re-added later and you want to call onLoad to reset the game? just curious what breaks if you just leave the camera there as is? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This camera isn't used, it uses several other cameras later instead. |
||
children.register<CameraComponent>(); | ||
cameraWorld = World(); | ||
add(cameraWorld); | ||
|
||
final walls = createWalls(trackSize); | ||
final bigBall = Ball(position: Vector2(200, 245), isMovable: false); | ||
cameraWorld.addAll([ | ||
final bigBall = Ball(initialPosition: Vector2(200, 245), isMovable: false); | ||
world.addAll([ | ||
LapLine(1, Vector2(25, 50), Vector2(50, 5), isFinish: false), | ||
LapLine(2, Vector2(25, 70), Vector2(50, 5), isFinish: false), | ||
LapLine(3, Vector2(52.5, 25), Vector2(5, 50), isFinish: true), | ||
|
@@ -82,9 +81,7 @@ class PadRacingGame extends Forge2DGame with KeyboardEvents { | |
canvasSize.x / trackSize.x, | ||
canvasSize.y / trackSize.y, | ||
); | ||
startCamera = CameraComponent( | ||
world: cameraWorld, | ||
) | ||
startCamera = CameraComponent(world: world) | ||
..viewfinder.position = trackSize / 2 | ||
..viewfinder.anchor = Anchor.center | ||
..viewfinder.zoom = zoomLevel - 0.2; | ||
|
@@ -136,7 +133,7 @@ class PadRacingGame extends Forge2DGame with KeyboardEvents { | |
..paint.style = PaintingStyle.stroke; | ||
final cameras = List.generate(numberOfPlayers, (i) { | ||
return CameraComponent( | ||
world: cameraWorld, | ||
world: world, | ||
viewport: FixedSizeViewport(viewportSize.x, viewportSize.y) | ||
..position = alignedVector( | ||
longMultiplier: i == 0 ? 0.0 : 1 / (i + 1), | ||
|
@@ -152,7 +149,7 @@ class PadRacingGame extends Forge2DGame with KeyboardEvents { | |
const mapCameraZoom = 0.5; | ||
final mapCameras = List.generate(numberOfPlayers, (i) { | ||
return CameraComponent( | ||
world: cameraWorld, | ||
world: world, | ||
viewport: FixedSizeViewport(mapCameraSize.x, mapCameraSize.y) | ||
..position = Vector2( | ||
viewportSize.x - mapCameraSize.x * mapCameraZoom - 50, | ||
|
@@ -193,7 +190,7 @@ class PadRacingGame extends Forge2DGame with KeyboardEvents { | |
} | ||
}); | ||
cars.add(car); | ||
cameraWorld.add(car); | ||
world.add(car); | ||
cameras[i].viewport.addAll([lapText, mapCameras[i]]); | ||
} | ||
|
||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -33,9 +33,9 @@ List<Wall> createWalls(Vector2 size) { | |
} | ||
|
||
class Wall extends BodyComponent<PadRacingGame> { | ||
Wall(this.position, this.size) : super(priority: 3); | ||
Wall(this._position, this.size) : super(priority: 3); | ||
|
||
final Vector2 position; | ||
final Vector2 _position; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I imagine things like this and initialPosition are to avoid conflict with a position defined on BodyComponent. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Because |
||
final Vector2 size; | ||
|
||
final Random rng = Random(); | ||
|
@@ -94,7 +94,7 @@ class Wall extends BodyComponent<PadRacingGame> { | |
Body createBody() { | ||
final def = BodyDef() | ||
..type = BodyType.static | ||
..position = position; | ||
..position = _position; | ||
final body = world.createBody(def) | ||
..userData = this | ||
..angularDamping = 3.0; | ||
|
@@ -105,7 +105,7 @@ class Wall extends BodyComponent<PadRacingGame> { | |
} | ||
|
||
late Rect asRect = Rect.fromCenter( | ||
center: position.toOffset(), | ||
center: _position.toOffset(), | ||
width: size.x, | ||
height: size.y, | ||
); | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
why the rename from gameRef to game?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
That is from the really old Pasha PR where we are moving from the
HasGameRef
mixin toHasGameReference
.