Skip to content
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!: Refactor the CameraComponent3D #3394

Merged
merged 6 commits into from
Dec 14, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
196 changes: 0 additions & 196 deletions packages/flame_3d/example/lib/keyboard_controlled_camera.dart

This file was deleted.

70 changes: 33 additions & 37 deletions packages/flame_3d/example/lib/main.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2,37 +2,25 @@ import 'dart:async';
import 'dart:math';

import 'package:example/crate.dart';
import 'package:example/keyboard_controlled_camera.dart';
import 'package:example/player_box.dart';
import 'package:example/rotating_light.dart';
import 'package:example/simple_hud.dart';
import 'package:example/touch_controlled_camera.dart';
import 'package:flame/events.dart';
import 'package:flame/extensions.dart' as v64 show Vector2;
import 'package:flame/game.dart' show FlameGame, GameWidget;
import 'package:flame/game.dart' show GameWidget;
import 'package:flame_3d/camera.dart';
import 'package:flame_3d/components.dart';
import 'package:flame_3d/game.dart';
import 'package:flame_3d/resources.dart';
import 'package:flutter/gestures.dart';
import 'package:flutter/material.dart' show runApp, Color, Colors, Listener;
import 'package:flutter/material.dart' show runApp, Color, Colors;

class ExampleGame3D extends FlameGame<World3D>
with HasKeyboardHandlerComponents {
class ExampleGame3D extends FlameGame3D<World3D, TouchControlledCamera>
with DragCallbacks, ScrollDetector {
ExampleGame3D()
: super(
world: World3D(clearColor: const Color(0xFFFFFFFF)),
camera: KeyboardControlledCamera(
viewport: FixedResolutionViewport(
resolution: v64.Vector2(800, 600),
),
hudComponents: [SimpleHud()],
),
camera: TouchControlledCamera(),
);

@override
KeyboardControlledCamera get camera =>
super.camera as KeyboardControlledCamera;

@override
FutureOr<void> onLoad() async {
world.addAll([
Expand Down Expand Up @@ -146,30 +134,38 @@ class ExampleGame3D extends FlameGame<World3D>
);
}
}

@override
void onScroll(PointerScrollInfo info) {
const scrollSensitivity = 0.01;
final delta = info.scrollDelta.global.y.clamp(-10, 10) * scrollSensitivity;

camera.distance += delta;
}

@override
void onDragUpdate(DragUpdateEvent event) {
camera.delta.setValues(event.deviceDelta.x, event.deviceDelta.y);
super.onDragUpdate(event);
}

@override
void onDragEnd(DragEndEvent event) {
camera.delta.setZero();
super.onDragEnd(event);
}

@override
void onDragCancel(DragCancelEvent event) {
camera.delta.setZero();
super.onDragCancel(event);
}
}

void main() {
final example = ExampleGame3D();

runApp(
Listener(
onPointerMove: (event) {
if (!event.down) {
return;
}
example.camera.pointerEvent = event;
},
onPointerSignal: (event) {
if (event is! PointerScrollEvent || !event.down) {
return;
}
example.camera.scrollMove = event.delta.dy / 3000;
},
onPointerUp: (event) => example.camera.pointerEvent = null,
onPointerCancel: (event) => example.camera.pointerEvent = null,
child: GameWidget(game: example),
),
);
runApp(GameWidget(game: example));
}

extension on Random {
Expand Down
7 changes: 1 addition & 6 deletions packages/flame_3d/example/lib/player_box.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ import 'dart:ui';

import 'package:example/main.dart';
import 'package:flame/components.dart' show HasGameReference;
import 'package:flame_3d/camera.dart';
import 'package:flame_3d/components.dart';
import 'package:flame_3d/game.dart';
import 'package:flame_3d/resources.dart';
Expand All @@ -20,10 +19,6 @@ class PlayerBox extends MeshComponent with HasGameReference<ExampleGame3D> {

@override
void renderTree(Canvas canvas) {
// Only show the box if we are in third person mode.
if (game.camera.mode == CameraMode.thirdPerson) {
position.setFrom(game.camera.target);
super.renderTree(canvas);
}
game.camera.target = position + Vector3(0, 2, 0);
}
}
1 change: 0 additions & 1 deletion packages/flame_3d/example/lib/simple_hud.dart
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,6 @@ Camera controls:
canvas,
'''
FPS: $fps
Mode: ${game.camera.mode.name}
Projection: ${game.camera.projection.name}
Culled: ${game.world.culled}

Expand Down
27 changes: 27 additions & 0 deletions packages/flame_3d/example/lib/touch_controlled_camera.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import 'package:example/simple_hud.dart';
import 'package:flame/extensions.dart' as v64 show Vector2;
import 'package:flame_3d/camera.dart';
import 'package:flame_3d/core.dart';

class TouchControlledCamera extends ThirdPersonCamera {
TouchControlledCamera()
: super(
following: Vector3(0, 2, 0),
followDamping: 1,
position: Vector3(0, 2, 4),
projection: CameraProjection.perspective,
distance: 3,
viewport: FixedResolutionViewport(
resolution: v64.Vector2(800, 600),
),
hudComponents: [SimpleHud()],
);

Vector2 delta = Vector2.zero();

@override
void update(double dt) {
super.update(dt);
rotate(delta.x * dt, delta.y * dt);
}
}
2 changes: 2 additions & 0 deletions packages/flame_3d/lib/camera.dart
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,6 @@ export 'package:flame/camera.dart';
export 'package:vector_math/vector_math.dart' show Frustum;

export 'src/camera/camera_component_3d.dart';
export 'src/camera/first_person_camera.dart';
export 'src/camera/third_person_camera.dart';
export 'src/camera/world_3d.dart';
Loading
Loading