Difficulty axes: the source
Open the live demo · Read the guide · View on GitHub
Regions the guide quotes: hit (line 83), apply (line 103), list (line 115), compare (line 126).
1/// Four axes a genre applies where it decides: what the player is hurt by,
2/// what their own attacks are worth, how quickly the opposition reacts, and
3/// how much help is switched on.
4///
5/// Quoted by `difficulty.md` and shown whole in the Source tab.
6library;
8import 'dart:math' as math;
10import 'package:flutter3d/flutter3d.dart';
11import 'package:flutter3d_showcase/src/demo/demo.dart';
12import 'package:flutter3d_showcase/src/demo/scene_kit.dart';
13import 'package:flutter3d_sim/flutter3d_sim.dart';
15/// The `difficulty` page: a target hit for ten points under each offered
16/// difficulty. Its check is that gentle softens the hit and punishing
17/// sharpens it.
18final class DifficultyDemo extends ShowcaseDemo {
19 /// The difficulty picked, an index into `Difficulty.offered`.
20 int level = 0;
22 late final MeshNode _player;
23 late final MeshNode _enemy;
24 late final BarGauge _health;
25 double _hp = 100.0;
26 double _swing = 0.0;
27 bool _struck = false;
30 void configureView(DemoContext context) {
31 context.orbit
32 ..distance = 9.0
33 ..pitch = 0.45
34 ..yaw = 0.0;
35 context.orbit.target.setValues(0.0, 0.8, 0.0);
36 }
39 Scene build(DemoContext context) {
40 _player = blockNode(
41 context,
42 'player',
43 Vector3(0.9, 1.6, 0.9),
44 Vector4(0.45, 0.65, 0.95, 1.0),
45 at: Vector3(-2.5, 0.8, 0.0),
46 );
47 _enemy = ballNode(
48 context,
49 'opponent',
50 0.6,
51 Vector4(0.85, 0.35, 0.3, 1.0),
52 at: Vector3(2.5, 0.6, 0.0),
53 );
54 _health = BarGauge(
55 context,
56 'health',
57 Vector4(0.4, 0.85, 0.4, 1.0),
58 Vector3(-3.5, 2.6, 0.0),
59 height: 2.0,
60 );
61 return sceneOf(<SceneNode>[
62 floorNode(context, width: 12.0, depth: 6.0),
63 _player,
64 _enemy,
65 ..._health.nodes,
66 ]);
67 }
70 void update(DemoContext context, double dt) {
71 final Difficulty difficulty = Difficulty.offered[level];
72 // The opponent lunges once a swing; a quicker opponent swings sooner.
73 _swing += dt * difficulty.opponentReaction * 0.6;
74 if (_swing >= 1.0) {
75 _swing -= 1.0;
76 _struck = false;
77 }
78 // Out to the player and back, hitting at the far end.
79 final double reach = math.sin(math.pi * _swing.clamp(0.0, 1.0));
80 _enemy.setPosition(2.5 - 4.2 * reach, 0.6, 0.0);
81 if (!_struck && reach > 0.97) {
82 _struck = true;
83 // The same ten-point hit as ever, scaled by the level.
84 _hp -= _incomingDamage(difficulty, 10.0);
85 if (_hp <= 0.0) _hp = 100.0;
86 }
87 _health.set(_hp / 100.0);
88 }
91 List<DemoControl> controls(DemoContext context) => <DemoControl>[
92 ChoiceControl(
93 'Difficulty',
94 options: <String>[for (final Difficulty d in Difficulty.offered) d.name],
95 index: () => level,
96 onChanged: (int i) {
97 level = i;
98 _hp = 100.0;
99 },
100 ),
101 ];
103 /// A hit a genre would deal at `normal`, scaled by one difficulty.
104 static double _incomingDamage(Difficulty at, double baseAmount) =>
105 baseAmount * at.damageTaken;
107 static String _lineFor(Difficulty level) {
108 final hit = _incomingDamage(level, 10.0);
109 return '${level.name}: a ten-point hit costs $hit, opponents react at '
110 'x${level.opponentReaction} speed, assistance ${level.assistance}';
111 }
113 /// One line for each level on offer, as the settings screen would list them.
114 static List<String> lines() {
115 final lines = <String>[
116 for (final Difficulty level in Difficulty.offered) _lineFor(level),
117 ];
118 return lines;
119 }
122 void verify(Scene scene, FrameResult frame) {
123 if (frame.drawCalls < 1) {
124 throw StateError('the target marker was not drawn');
125 }
126 final onGentle = _incomingDamage(Difficulty.gentle, 10.0);
127 final onPunishing = _incomingDamage(Difficulty.punishing, 10.0);
128 if (!(onGentle < 10.0 && onPunishing > 10.0)) {
129 throw StateError(
130 'gentle should soften a hit and punishing should '
131 'sharpen it, against the same ten points',
132 );
133 }
134 }
135}