Skip to content

Commit

Permalink
Fix collisions again
Browse files Browse the repository at this point in the history
  • Loading branch information
CodeDoctorDE committed Dec 13, 2023
1 parent 42b22e8 commit 6b5f2b3
Show file tree
Hide file tree
Showing 10 changed files with 453 additions and 34 deletions.
13 changes: 8 additions & 5 deletions app/lib/game/board.dart
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,6 @@ 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 SpacedSpriteSheet {
final ui.Image image;
final Vector2 srcSize, spacing, margin;
Expand All @@ -38,14 +36,15 @@ class SpacedSpriteSheet {

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

final Vector2 _tileSize = Vector2.all(16);
Vector2 get tileSize => _tileSize;
final VoidCallback onEscape;

BoardGame({
required this.networkingService,
required this.onEscape,
});

@override
Expand All @@ -67,7 +66,6 @@ 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.viewport.add(InventoryHud());
camera.follow(_player.positionProvider);
Expand All @@ -77,7 +75,7 @@ class BoardGame extends FlameGame with KeyboardEvents, HasCollisionDetection {
for (final object
in component.tileMap.getLayer<ObjectGroup>('Collisions')?.objects ??
<TiledObject>[]) {
collisions.add(BoardWall(
world.add(BoardWall(
position: Vector2(object.x, object.y),
size: Vector2(object.width, object.height),
));
Expand Down Expand Up @@ -116,6 +114,11 @@ class BoardGame extends FlameGame with KeyboardEvents, HasCollisionDetection {
handled = true;
}
movement.normalize();
if (event is RawKeyUpEvent &&
event.logicalKey == LogicalKeyboardKey.escape) {
onEscape();
movement = Vector3.zero();
}
if (event is RawKeyUpEvent &&
event.logicalKey == LogicalKeyboardKey.space) {
_player.toggleSit();
Expand Down
6 changes: 4 additions & 2 deletions app/lib/game/inventory.dart
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import 'dart:ui';
import 'package:flame/components.dart';
import 'package:flame/events.dart';
import 'package:flame/flame.dart';
import 'package:flame/input.dart';
import 'package:qeck/game/board.dart';

class HudSpriteSheet {
Expand Down Expand Up @@ -59,7 +58,10 @@ class QuickHud extends Component with HasGameRef {
}
}

class InventoryScreen extends Component {}
class InventoryScreen extends Component {
@override
Future<void> onLoad() async {}
}

class InventoryItem extends SpriteComponent with HoverCallbacks {
final HudSpriteSheet _spriteSheet;
Expand Down
90 changes: 72 additions & 18 deletions app/lib/game/player.dart
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,11 @@ import 'dart:ui' as ui;
import 'package:flame/collisions.dart';
import 'package:flame/components.dart';
import 'package:flame/effects.dart';
import 'package:flame/extensions.dart';
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 @@ -56,6 +57,10 @@ class BoardPlayer
ReadOnlyPositionProvider get positionProvider =>
_PreviousPlayerPositionComponent(this);

final RectangleHitbox collisionHitbox =
RectangleHitbox(position: Vector2.all(2), size: Vector2.all(12))
..debugMode = true;

@override
Future<void> onLoad() async {
size = game.tileSize;
Expand All @@ -79,10 +84,7 @@ class BoardPlayer
);

add(_text);
final realSize = Vector2.all(16);
add(RectangleHitbox(
size: realSize,
));
add(collisionHitbox);
}

Map<(PlayerState, PlayerDirection), SpriteAnimation> _getAnimations() {
Expand Down Expand Up @@ -121,27 +123,27 @@ class BoardPlayer
);
}

Vector2 velocity = Vector2.zero(), previousPosition = Vector2.zero();
Vector2 velocity = Vector2.zero();
PlayerDirection get direction => current?.$2 ?? PlayerDirection.front;
PlayerState get state => current?.$1 ?? PlayerState.idle;
final double _speed = 50;
final Set<PositionComponent> _collidesXPos = {},
_collidesXNeg = {},
_collidesYPos = {},
_collidesYNeg = {};

@override
void update(double dt) {
previousPosition = position.clone();
final next = velocity.xy * dt * _speed;
final length = next.length;
if (length != 0) {
final ray = Ray2(
direction: next.normalized(),
origin: position,
);
final result = game.collisions.collisionDetection
.raycast(ray, maxDistance: length + 4);
if (result == null) {
position.add(next);
}
if (_collidesXPos.isNotEmpty && next.x > 0 ||
_collidesXNeg.isNotEmpty && next.x < 0) {
next.x = 0;
}
if (_collidesYPos.isNotEmpty && next.y > 0 ||
_collidesYNeg.isNotEmpty && next.y < 0) {
next.y = 0;
}
position.add(next);
if (state != PlayerState.sitting) {
if (velocity.x == 0 && velocity.y == 0) {
current = (PlayerState.idle, direction);
Expand Down Expand Up @@ -187,4 +189,56 @@ class BoardPlayer
velocity = Vector2.zero();
}
}

void _toggleSetElement<T>(Set<T> set, T element, bool value) {
if (value) {
set.add(element);
} else {
set.remove(element);
}
}

@override
void onCollision(Set<Vector2> intersectionPoints, PositionComponent other) {
if (other is BoardWall) {
other.debugMode = true;
collisionHitbox.debugMode = true;

Rect intersection =
collisionHitbox.toAbsoluteRect().intersect(other.toAbsoluteRect());
final dx = intersection.width;
final dy = intersection.height;
if (dx > dy) {
if (position.y < other.position.y) {
position.y -= dy;
} else {
position.y += dy;
}
} else {
if (position.x < other.position.x) {
position.x -= dx;
} else {
position.x += dx;
}
}
_toggleSetElement(
_collidesYPos, other, dx > dy && position.y < other.position.y);
_toggleSetElement(
_collidesYNeg, other, dx > dy && position.y > other.position.y);
_toggleSetElement(
_collidesXPos, other, dx < dy && position.x < other.position.x);
_toggleSetElement(
_collidesXNeg, other, dx < dy && position.x > other.position.x);
}
super.onCollision(intersectionPoints, other);
}

@override
void onCollisionEnd(PositionComponent other) {
super.onCollisionEnd(other);
_collidesXPos.remove(other);
_collidesXNeg.remove(other);
_collidesYPos.remove(other);
_collidesYNeg.remove(other);
}
}
3 changes: 2 additions & 1 deletion app/lib/l10n/app_en.arb
Original file line number Diff line number Diff line change
Expand Up @@ -72,5 +72,6 @@
"locale": "Locale",
"nativeTitleBar": "Native window title bar",
"board": "Board",
"home": "Home"
"home": "Home",
"disconnect": "Disconnect"
}
2 changes: 1 addition & 1 deletion app/lib/main.dart
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,7 @@ class FlowApp extends StatelessWidget {
GoRoute(
path: 'board',
pageBuilder: _fadeTransitionBuilder(
(context, state) => const BoardPage(),
(context, state) => BoardPage(),
),
),
GoRoute(
Expand Down
17 changes: 17 additions & 0 deletions app/lib/models/event.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import 'package:dart_mappable/dart_mappable.dart';

part 'event.mapper.dart';

@MappableClass(discriminatorKey: 'type')
class NetworkingEvent with NetworkingEventMappable {}

@MappableClass(discriminatorValue: 'move')
class NetworkingMoveEvent extends NetworkingEvent
with NetworkingMoveEventMappable {
final (double, double) position, velocity;

NetworkingMoveEvent({
required this.position,
required this.velocity,
});
}
Loading

1 comment on commit 6b5f2b3

@vercel
Copy link

@vercel vercel bot commented on 6b5f2b3 Dec 13, 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-linwood.vercel.app
qeck.linwood.dev
qeck.vercel.app

Please sign in to comment.