Skip to content

Commit

Permalink
Add sitting
Browse files Browse the repository at this point in the history
  • Loading branch information
CodeDoctorDE committed Dec 12, 2023
1 parent d99087d commit 4e5df79
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 10 deletions.
17 changes: 15 additions & 2 deletions app/lib/game/board.dart
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import 'dart:async';

import 'package:flame/components.dart';
import 'package:flame/events.dart';
import 'package:flame/experimental.dart';
import 'package:flame/game.dart';
Expand All @@ -10,8 +11,11 @@ import 'package:qeck/game/player.dart';
import 'package:qeck/game/wall.dart';
import 'package:qeck/services/network.dart';

class BoardCollisions extends Component with HasCollisionDetection {}

class BoardGame extends FlameGame with KeyboardEvents, HasCollisionDetection {
final NetworkingService networkingService;
final BoardCollisions collisions = BoardCollisions();
final BoardPlayer _player = BoardPlayer();

final Vector2 _tileSize = Vector2.all(16);
Expand Down Expand Up @@ -40,6 +44,7 @@ class BoardGame extends FlameGame with KeyboardEvents, HasCollisionDetection {
_player.position = Vector2(spawn?.x ?? 0, spawn?.y ?? 0);

world.add(component);
world.add(collisions);
world.add(_player);
camera.follow(_player.positionProvider);
camera.viewfinder.zoom = 8;
Expand All @@ -48,7 +53,7 @@ class BoardGame extends FlameGame with KeyboardEvents, HasCollisionDetection {
for (final object
in component.tileMap.getLayer<ObjectGroup>('Collisions')?.objects ??
<TiledObject>[]) {
world.add(BoardWall(
collisions.add(BoardWall(
position: Vector2(object.x, object.y),
size: Vector2(object.width, object.height),
));
Expand Down Expand Up @@ -87,7 +92,15 @@ class BoardGame extends FlameGame with KeyboardEvents, HasCollisionDetection {
handled = true;
}
movement.normalize();
_player.move(movement);
if (keysPressed.contains(LogicalKeyboardKey.shiftLeft)) {
movement *= 2;
}
if (event is RawKeyUpEvent &&
event.logicalKey == LogicalKeyboardKey.space) {
_player.toggleSit();
} else {
_player.move(movement);
}
return handled ? KeyEventResult.handled : KeyEventResult.ignored;
}
}
29 changes: 21 additions & 8 deletions app/lib/game/player.dart
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ import 'package:flame/flame.dart';
import 'package:flame/geometry.dart';
import 'package:flame/text.dart';
import 'package:qeck/game/board.dart';
import 'package:qeck/game/wall.dart';
import 'package:qeck/services/network.dart';

class _PreviousPlayerPositionComponent extends ReadOnlyPositionProvider {
Expand Down Expand Up @@ -141,24 +140,25 @@ class BoardPlayer
direction: next.normalized(),
origin: position,
);
final result =
game.collisionDetection.raycast(ray, maxDistance: length + 4);
final result = game.collisions.collisionDetection
.raycast(ray, maxDistance: length + 4);
if (result == null) {
position.add(next);
}
}
if (velocity.x == 0 && velocity.y == 0) {
current = (PlayerState.idle, direction);
} else {
current = (PlayerState.walking, direction);
if (state != PlayerState.sitting) {
if (velocity.x == 0 && velocity.y == 0) {
current = (PlayerState.idle, direction);
} else {
current = (PlayerState.walking, direction);
}
}
super.update(dt);
}

bool get wasFlipped => direction == PlayerDirection.left;

void move(Vector3 velocity) {
this.velocity = velocity.xy;
final lastFlipped = wasFlipped;
if (velocity.x > 0) {
current = (state, PlayerDirection.right);
Expand All @@ -171,5 +171,18 @@ class BoardPlayer
flipHorizontally();
_text.flipHorizontally();
}
if (state == PlayerState.sitting) {
return;
}
this.velocity = velocity.xy;
}

void toggleSit() {
if (state == PlayerState.sitting) {
current = (PlayerState.idle, direction);
} else {
current = (PlayerState.sitting, direction);
velocity = Vector2.zero();
}
}
}

1 comment on commit 4e5df79

@vercel
Copy link

@vercel vercel bot commented on 4e5df79 Dec 12, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Successfully deployed to the following URLs:

qeck – ./app

qeck-git-develop-linwood.vercel.app
qeck.vercel.app
qeck-linwood.vercel.app
qeck.linwood.dev

Please sign in to comment.