Skip to content

Instantly share code, notes, and snippets.

View identity2's full-sized avatar
🍇
grapes

Ramen identity2

🍇
grapes
View GitHub Profile
# 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):
# 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)
# 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
//the update function of music notes
void Update()
{
transform.position = Vector2.Lerp(
SpawnPos,
RemovePos,
(BeatsShownInAdvance - (beatOfThisNote - songPosInBeats)) / BeatsShownInAdvance
);
}
if (nextIndex < notes.Length && notes[nextIndex] < songPosInBeats + beatsShownInAdvance)
{
Instantiate( /* Music Note Prefab */ );
//initialize the fields of the music note
nextIndex++;
}
void Update()
{
//calculate the position in seconds
songPosition = (float) (AudioSettings.dspTime - dsptimesong);
//calculate the position in beats
songPosInBeats = songPosition / secPerBeat;
}
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
@identity2
identity2 / variables.cs
Created June 8, 2019 12:50
rhythm game
//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
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;
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)