This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# This function should be called whenever the character moves. | |
func update_camera(character_pos): | |
var new_camera_pos = self.get_global_pos() | |
# Check if the character reaches the right margin. | |
if character_pos.x > self.get_global_pos().x + screen_size.width * (drag_margin_right - 0.5): | |
new_camera_pos.x = character_pos.x - screen_size.width * (drag_margin_right - 0.5) | |
# Check if the character reaches the left margin. | |
elif character_pos.x < self.get_global_pos().x + screen_size.width * (drag_margin_left - 0.5): |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Being called when loaded. | |
func _ready(): | |
screen_size = self.get_viewport_rect().size | |
# Actually scroll the screen. (Update the viewport according to the position of the camera.) | |
func update_viewport(): | |
var canvas_transform = self.get_viewport().get_canvas_transform() | |
canvas_transform.o = -self.get_global_pos() + screen_size / 2.0 | |
self.get_viewport().set_canvas_transform(canvas_transform) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Scroll the screen (aka. move the camera) when the character reaches the margins. | |
var drag_margin_left = 0.3 | |
var drag_margin_right = 0.7 | |
# The left/right most edge of the scene. (The camera couldn't move past these limits.) | |
var right_limit = 1000000 | |
var left_limit = -1000000 | |
# The size of the screen | |
var screen_size |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//the update function of music notes | |
void Update() | |
{ | |
transform.position = Vector2.Lerp( | |
SpawnPos, | |
RemovePos, | |
(BeatsShownInAdvance - (beatOfThisNote - songPosInBeats)) / BeatsShownInAdvance | |
); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
if (nextIndex < notes.Length && notes[nextIndex] < songPosInBeats + beatsShownInAdvance) | |
{ | |
Instantiate( /* Music Note Prefab */ ); | |
//initialize the fields of the music note | |
nextIndex++; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
void Update() | |
{ | |
//calculate the position in seconds | |
songPosition = (float) (AudioSettings.dspTime - dsptimesong); | |
//calculate the position in beats | |
songPosInBeats = songPosition / secPerBeat; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
void Start() | |
{ | |
//calculate how many seconds is one beat | |
//we will see the declaration of bpm later | |
secPerBeat = 60f / bpm; | |
//record the time when the song starts | |
dsptimesong = (float) AudioSettings.dspTime; | |
//start the song |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//the current position of the song (in seconds) | |
float songPosition; | |
//the current position of the song (in beats) | |
float songPosInBeats; | |
//the duration of a beat | |
float secPerBeat; | |
//how much time (in seconds) has passed since the song started |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public static void SaveAudioClipToDisk(AudioClip audioClip, string filename) | |
{ | |
//create file | |
BinaryFormatter bf = new BinaryFormatter (); | |
FileStream file = File.Create (Application.persistentDataPath+ "/" + filename); | |
//serialize by setting the sample, frequency, samples, and channels to the new AudioClipSample instance | |
AudioClipSample newSample = new AudioClipSample(); | |
newSample.sample = new float[audioClip.samples * audioClip.channels]; | |
newSample.frequency = audioClip.frequency; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public static void LoadAudioClipFromDisk(AudioSource audioSource, string filename) | |
{ | |
if (File.Exists (Application.persistentDataPath + "/"+ filename)) { | |
//deserialize local binary file to AudioClipSample | |
BinaryFormatter bf = new BinaryFormatter (); | |
FileStream file = File.Open (Application.persistentDataPath + "/" + filename, FileMode.Open); | |
AudioClipSample clipSample = (AudioClipSample) bf.Deserialize (file); | |
file.Close (); | |
//create new AudioClip instance, and set the (name, samples, channels, frequency, [stream] play immediately without fully loaded) |
NewerOlder