makes camera follow player
MODIFY
example/lib/main.dart
example/lib/main.dart
@@ -61,6 +61,8 @@ class Game extends StatelessWidget implements GameListener {
background: BackgroundColorGame(Colors.blueGrey[900]),
gameController: _controller..setListener(this),
lightingColorGame: Colors.black.withOpacity(0.4),
+ showCollisionArea: true,
+ constructionMode: true,
);
}

MODIFY
example/lib/player/knight.dart
example/lib/player/knight.dart
@@ -97,8 +97,8 @@ class Knight extends SimplePlayer with WithLighting {
gameRef.addDecoration(
GameDecoration(
initPosition: Position(
- positionInWorld.left,
- positionInWorld.top,
+ position.center.dx,
+ position.center.dy,
),
height: 30,
width: 30,
@@ -177,8 +177,9 @@ class Knight extends SimplePlayer with WithLighting {
@override
void update(double dt) {
- if (this.isDead) return;
+ if (this.isDead || gameRef?.size == null) return;
_verifyStamina();
+ this.gameRef.gameCamera.position = Position(position.center.dx - gameRef.size.width / 2, position.center.dy - gameRef.size.height / 2);
this.seeEnemy(
visionCells: 8,
notObserved: () {

MODIFY
lib/player/player.dart
lib/player/player.dart
@@ -27,7 +27,7 @@ class Player extends AnimatedObject
bool _isDead = false;
- bool _usePositionInWorld = true;
+ bool _usePositionInWorld = false;
bool _nextFrameUsePosition = false;
final Size sizeCentralMovementWindow;
@@ -73,75 +73,44 @@ class Player extends AnimatedObject
}
void moveTop(double speed) {
- if (position.top <= 0) return;
-
double innerSpeed = speed * _dtUpdate;
Rect displacement = position.translate(0, (innerSpeed * -1));
if (isCollision(displacement, gameRef)) return;
- if (position.top >= rectCentralMovementWindow.top ||
- gameRef.gameCamera.isMaxTop()) {
position = displacement;
- } else {
- gameRef.gameCamera
- .moveCamera(innerSpeed, JoystickMoveDirectional.MOVE_UP);
- }
}
void moveRight(double speed) {
- if (position.right >= gameRef.size.width) return;
-
double innerSpeed = speed * _dtUpdate;
Rect displacement = position.translate(innerSpeed, 0);
if (isCollision(displacement, gameRef)) return;
- if (position.right <= rectCentralMovementWindow.right ||
- gameRef.gameCamera.isMaxRight()) {
position = displacement;
- } else {
- gameRef.gameCamera
- .moveCamera(innerSpeed, JoystickMoveDirectional.MOVE_RIGHT);
- }
+
}
void moveBottom(double speed) {
- if (position.bottom >= gameRef.size.height) return;
-
double innerSpeed = speed * _dtUpdate;
Rect displacement = position.translate(0, innerSpeed);
if (isCollision(displacement, gameRef)) return;
- if (position.bottom <= rectCentralMovementWindow.bottom ||
- gameRef.gameCamera.isMaxBottom()) {
position = displacement;
- } else {
- gameRef.gameCamera
- .moveCamera(innerSpeed, JoystickMoveDirectional.MOVE_DOWN);
- }
}
void moveLeft(double speed) {
- if (position.left <= 0) return;
-
double innerSpeed = speed * _dtUpdate;
Rect displacement = position.translate(innerSpeed * -1, 0);
if (isCollision(displacement, gameRef)) return;
- if (position.left >= rectCentralMovementWindow.left ||
- gameRef.gameCamera.isMaxLeft()) {
position = displacement;
- } else {
- gameRef.gameCamera
- .moveCamera(innerSpeed, JoystickMoveDirectional.MOVE_LEFT);
- }
}
void moveFromAngle(double speed, double angle) {
@@ -255,7 +224,7 @@ class Player extends AnimatedObject
}
void usePositionInWorldToRender() {
- _usePositionInWorld = true;
+ _usePositionInWorld = false;
}
void usePositionToRender() {
@@ -264,7 +233,7 @@ class Player extends AnimatedObject
@override
get positionInWorld {
- if (_usePositionInWorld) return super.positionInWorld;
+ if (_usePositionInWorld) return super.position;
return super.positionInWorld = Rect.fromLTWH(
position.left - gameRef.gameCamera.position.x,

MODIFY
lib/util/world_component.dart
lib/util/world_component.dart
@@ -16,7 +16,7 @@ abstract class WorldComponent extends GameComponent {
@override
void update(double t) {
- position = positionInWordToPosition();
+ position ??= positionInWorld ?? Rect.zero;
}
@override
@@ -30,24 +30,16 @@ abstract class WorldComponent extends GameComponent {
}
bool isVisibleInMap() {
- if (gameRef == null || gameRef.size == null || position == null)
+ if (gameRef?.size == null || position == null || destroy() == true)
return false;
- return position.top < (gameRef.size.height + position.height) &&
- position.top > (position.height * -1) &&
- position.left > (position.width * -1) &&
- position.left < (gameRef.size.width + position.width) &&
- !destroy();
- }
-
- Rect positionInWordToPosition() {
- if (positionInWorld == null) return Rect.zero;
- if (gameRef == null) return positionInWorld;
- return Rect.fromLTWH(
- positionInWorld.left + gameRef.gameCamera.position.x,
- positionInWorld.top + gameRef.gameCamera.position.y,
- positionInWorld.width,
- positionInWorld.height,
+ final cameraRect = Rect.fromLTWH(
+ gameRef.gameCamera.position.x,
+ gameRef.gameCamera.position.y,
+ gameRef.size.width,
+ gameRef.size.height,
);
+
+ return position.overlaps(cameraRect);
}
}

makes camera follow player