Skip to content

Commit

Permalink
feat: show reflection in aim guide
Browse files Browse the repository at this point in the history
  • Loading branch information
adil192 committed Jul 16, 2024
1 parent b850826 commit 41a9ba7
Show file tree
Hide file tree
Showing 9 changed files with 38 additions and 31 deletions.
62 changes: 34 additions & 28 deletions lib/flame/components/aim_guide.dart
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import 'package:flame/components.dart';
import 'package:flame/extensions.dart';
import 'package:flutter/material.dart';
import 'package:ricochlime/flame/components/bullet.dart';
import 'package:ricochlime/flame/components/walls.dart';
import 'package:ricochlime/flame/ricochlime_game.dart';

/// A component that draws a dotted line
Expand All @@ -24,9 +25,6 @@ class AimGuide extends PositionComponent with HasGameRef<RicochlimeGame> {
/// The maximum number of dots to be drawn.
static const _maxDots = 20;

/// A time value between 0 and 1 used to animate the aim guide.
double t = 0;

@override
Future<void> onLoad() async {
await super.onLoad();
Expand All @@ -38,14 +36,6 @@ class AimGuide extends PositionComponent with HasGameRef<RicochlimeGame> {
priority = 2;
}

@override
void update(double dt) {
t = (RicochlimeGame.reduceMotion || RicochlimeGame.reproducibleGoldenMode)
? 0.5
: (t + dt) % 1;
super.update(dt);
}

@override
void render(Canvas canvas) {
super.render(canvas);
Expand All @@ -55,29 +45,34 @@ class AimGuide extends PositionComponent with HasGameRef<RicochlimeGame> {
return;
}

const gameInset = Bullet.radius + wallInset;
const gameRect = Rect.fromLTRB(
Bullet.radius,
Bullet.radius,
RicochlimeGame.expectedWidth - Bullet.radius,
RicochlimeGame.expectedHeight - Bullet.radius,
gameInset,
gameInset,
RicochlimeGame.expectedWidth - gameInset,
RicochlimeGame.expectedHeight - gameInset,
);

/// The offset between two consecutive dots.
final dotDeltaPosition = aimDetails.unitDir.toOffset() * _dotInterval;

/// The dot's position relative to the player.
var dotLocalPosition = dotDeltaPosition * t;
/// The position where the aim guide hits the left or right wall,
/// relative to the player.
final reflectionPoint = aimDetails.reflectionPoint(position, gameRect);
final reflectionDist = reflectionPoint.length;

/// The dot's position in game coordinates.
var dotGlobalPosition = position.toOffset() + dotLocalPosition;
final numDotsTotal = (aimDetails.aimLength * _maxDots).ceil();
final numDotsBeforeReflection = (reflectionDist / _dotInterval).floor();
final numDotsAfterReflection = numDotsTotal - numDotsBeforeReflection;

for (var dot = 0; dot < _maxDots * aimDetails.aimLength; dot++) {
dotLocalPosition += dotDeltaPosition;
dotGlobalPosition += dotDeltaPosition;

Bullet.drawBullet(canvas, dotLocalPosition.toVector2(), opacity: 0.9);
final adjustedDotInterval = reflectionDist / numDotsBeforeReflection;
final dotDelta = aimDetails.unitDir * adjustedDotInterval;
for (double dot = 0; dot < numDotsBeforeReflection; dot++) {
final dotPosition = dotDelta * dot;
Bullet.drawBullet(canvas, dotPosition, opacity: 1);
}

if (!gameRect.contains(dotGlobalPosition)) break;
dotDelta.x *= -1; // reflect
for (double dot = 0; dot < numDotsAfterReflection; dot++) {
final dotPosition = reflectionPoint + dotDelta * dot;
Bullet.drawBullet(canvas, dotPosition, opacity: 0.8);
}
}

Expand Down Expand Up @@ -169,4 +164,15 @@ class AimDetails {
/// This is used to let the player cancel the aim
/// by moving the mouse to the other side of the player.
bool mouseBelowPlayer;

/// Returns the point (relative to the player)
/// where the aim guide hits the left or right wall,
/// beginning from the player's position and in the direction of [unitDir].
Vector2 reflectionPoint(Vector2 playerPos, Rect gameRect) {
if (unitDir.x == 0) return Vector2(0, double.infinity);

final y = unitDir.y / unitDir.x.abs() * gameRect.width / 2;
final x = unitDir.x > 0 ? gameRect.right : gameRect.left;
return Vector2(x - playerPos.x, y);
}
}
7 changes: 4 additions & 3 deletions lib/flame/components/walls.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,19 @@

import 'package:flame_forge2d/flame_forge2d.dart';

const wallInset = 2.0;

/// Creates the [Wall]s that make up the boundaries of the world.
List<Wall> createBoundaries(
double width,
double height, {
double inset = 2,
bool includeTop = true,
bool includeBottom = true,
bool includeLeft = true,
bool includeRight = true,
}) {
late final topLeft = Vector2(inset, inset);
late final bottomRight = Vector2(width - inset, height - inset);
late final topLeft = Vector2(wallInset, wallInset);
late final bottomRight = Vector2(width - wallInset, height - wallInset);
late final topRight = Vector2(bottomRight.x, topLeft.y);
late final bottomLeft = Vector2(topLeft.x, bottomRight.y);

Expand Down
Binary file modified metadata/en-US/images/macbookScreenshots/2_play.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified metadata/en-US/images/newerIpadScreenshots/2_play.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified metadata/en-US/images/newerIphoneScreenshots/2_play.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified metadata/en-US/images/olderIpadScreenshots/2_play.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified metadata/en-US/images/olderIphoneScreenshots/2_play.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified metadata/en-US/images/phoneScreenshots/2_play.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified metadata/en-US/images/tenInchScreenshots/2_play.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit 41a9ba7

Please sign in to comment.