Skip to content
This repository has been archived by the owner on Dec 18, 2023. It is now read-only.

Commit

Permalink
Add background music
Browse files Browse the repository at this point in the history
  • Loading branch information
ufrshubham committed Dec 10, 2023
1 parent 8f7c09e commit 5ca3542
Show file tree
Hide file tree
Showing 8 changed files with 36 additions and 23 deletions.
Binary file added assets/audio/HoldLabs-Music.mp3
Binary file not shown.
Binary file modified assets/images/Tiles.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
2 changes: 1 addition & 1 deletion assets/tiles/Level2.tmx
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@
</properties>
<point/>
</object>
<object id="18" name="LevelComplete" type="Trigger" x="216" y="208" width="40" height="48">
<object id="18" name="Button Instructions" type="Trigger" x="208" y="208" width="48" height="48">
<properties>
<property name="Audio" type="file" value="../audio/Level2-ButtonTrigger.mp3"/>
<property name="HoldInput" type="bool" value="true"/>
Expand Down
9 changes: 7 additions & 2 deletions lib/game/game.dart
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
import 'dart:async';
import 'dart:math';

import 'package:flame/components.dart';
import 'package:flame/game.dart';
import 'package:flame/input.dart';
import 'package:flame_audio/flame_audio.dart';
import 'package:flutter/foundation.dart';
import 'package:flutter/material.dart';
import 'package:hold_labs/game/level.dart';
Expand Down Expand Up @@ -35,12 +37,15 @@ class HoldLabsGame extends FlameGame
'Buttons.png',
],
);
changeLevel(2);

FlameAudio.bgm.initialize();
await FlameAudio.bgm.play('HoldLabs-Music.mp3', volume: 0.8);
changeLevel(1);
}

void changeLevel(int levelId) {
currentLevel?.removeFromParent();
currentLevel = Level(levelId);
currentLevel = Level(min(levelId, 2));
world.add(currentLevel!);
}
}
12 changes: 7 additions & 5 deletions lib/game/level.dart
Original file line number Diff line number Diff line change
Expand Up @@ -170,6 +170,7 @@ class Level extends PositionComponent with HasGameReference<HoldLabsGame> {
?.split('audio/')
.last;
if (audioPath != null) {
FlameAudio.bgm.pause();
_player.moveLock = true;

await FlameAudio.audioCache.load(audioPath);
Expand All @@ -179,6 +180,7 @@ class Level extends PositionComponent with HasGameReference<HoldLabsGame> {
audioplayer.onPlayerComplete.listen(
(event) {
_player.moveLock = false;
FlameAudio.bgm.resume();
},
);
}
Expand Down Expand Up @@ -237,15 +239,15 @@ class Level extends PositionComponent with HasGameReference<HoldLabsGame> {
audioTrigger.removeFromParent();

if (holdInput) {
FlameAudio.bgm.pause();
_player.moveLock = true;
}

final audioplayer = await FlameAudio.playLongAudio(filename);
if (holdInput) {
audioplayer.onPlayerComplete.listen((event) {
_player.moveLock = false;
});
}
audioplayer.onPlayerComplete.listen((event) {
_player.moveLock = false;
FlameAudio.bgm.resume();
});
}
}

Expand Down
10 changes: 8 additions & 2 deletions lib/game/player.dart
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,13 @@ class Player extends PositionComponent

bool jump = false;
bool _isOnGround = false;
bool moveLock = false;
bool _moveLock = false;
set moveLock(bool value) {
_moveLock = value;
if (_moveLock) {
hAxisValue = 0;
}
}

final _gravity = 10 * 60.0;
final _moveSpeed = 100.0;
Expand Down Expand Up @@ -170,7 +176,7 @@ class Player extends PositionComponent
hAxisValue = 0;
vAxisValue = 0;

if (!moveLock) {
if (!_moveLock) {
hAxisValue += keysPressed.contains(LogicalKeyboardKey.keyA) ? -1 : 0;
hAxisValue += keysPressed.contains(LogicalKeyboardKey.keyD) ? 1 : 0;

Expand Down
2 changes: 1 addition & 1 deletion tiled-project/Level2.tmx
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@
</properties>
<point/>
</object>
<object id="18" name="LevelComplete" type="Trigger" x="216" y="208" width="40" height="48">
<object id="18" name="Button Instructions" type="Trigger" x="208" y="208" width="48" height="48">
<properties>
<property name="Audio" type="file" value="../assets/audio/Level2-ButtonTrigger.mp3"/>
<property name="HoldInput" type="bool" value="true"/>
Expand Down
24 changes: 12 additions & 12 deletions tiled-project/hold-labs.tiled-session
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
"height": 4300,
"width": 2
},
"activeFile": "Level1.tmx",
"activeFile": "Level2.tmx",
"expandedProjectPaths": [
"."
],
Expand All @@ -15,14 +15,14 @@
},
"Level1.tmx": {
"expandedObjectLayers": [
4,
2
2,
4
],
"scale": 1,
"selectedLayer": 1,
"viewCenter": {
"x": 265.5,
"y": 100
"x": 264.5,
"y": 99
}
},
"Level2.tmx": {
Expand All @@ -31,16 +31,16 @@
4,
2
],
"scale": 4,
"selectedLayer": 2,
"scale": 0.75,
"selectedLayer": 3,
"viewCenter": {
"x": 273.125,
"y": 230.5
"x": 372.6666666666667,
"y": 277.33333333333337
}
},
"Tiles.tsx": {
"dynamicWrapping": true,
"scaleInDock": 2,
"scaleInDock": 4,
"scaleInEditor": 4
}
},
Expand All @@ -54,14 +54,14 @@
"map.width": 50,
"openFiles": [
"Level1.tmx",
"Bricks.tsx"
"Level2.tmx"
],
"project": "hold-labs.tiled-project",
"property.type": "bool",
"recentFiles": [
"Level1.tmx",
"Level2.tmx",
"Tiles.tsx",
"Level1.tmx",
"Bricks.tsx"
],
"tileset.lastUsedFormat": "tsx",
Expand Down

0 comments on commit 5ca3542

Please sign in to comment.