//public Task GetDetectionTask() //{ //} public void DetectAndReportBeats() { detector.setStarted(true); while (true) { detector.update(); lastBeat = detector.getLastBeat(); //I'm sure no song has a bpm higher than 6000 so //it's safe to sleep 10ms before checking if a new beat has been detected //This will also save a lot of processing power. Thread.Sleep(10); if (detector.getLastBeat() != null && lastBeat != null) { if (lastBeat.getMilliseconds() != detector.getLastBeat().getMilliseconds()) { reportLastBeat(); } } } }
public virtual void Update(GameTime gameTime) { Music?.update(); bool exitPressed = InputManager.GetValue("exit"); if (exitPressed && !prevExitPressed) { PauseMusic(); Game.CurrentRoom = Parent; } prevExitPressed = exitPressed; if (TimeBegan == 0) { //TimeBegan = (float)gameTime.TotalGameTime.TotalMilliseconds; PlayMusic(); } if (TimeModifier == 0) { return; } float delta = (float)gameTime.ElapsedGameTime.TotalMilliseconds; //TimeElapsed += delta * TimeModifier; //TimeElapsed = (float)gameTime.TotalGameTime.TotalMilliseconds - TimeBegan; //time elapsed now takes from Music keyPresses = new bool[4]; for (int i = 0; i < Columns.Length; i++) { keyPresses[i] = InputManager.GetValue("column" + i.ToString()); } if (receiveInput) { for (int i = 0; i < Columns.Length; i++) { List <float> column = Columns[i]; if (keyPresses[i] && !prevKeyPresses[i]) { //we only care about the note closest to us if (column.Count == 0) { continue; } int ind = 0; //given list is sorted smallest to largest int noteY = TimeToPosition(column[ind]); int centerY = noteY + (NoteHeight / 2); int halfPressableHeight = NotePressableHeight / 2; int dist = Math.Max(Math.Abs(centerY - boardHeight) - (NoteHeight / 2), 0); //-noteheight/2 because it its perfect if we hit directly on the note if (centerY - halfPressableHeight <= boardHeight && centerY + halfPressableHeight >= boardHeight) { //we have a hit //calculate score float accuracy = CalculateAccuracy(dist); totalAccuracy += accuracy; int score = (int)(accuracy * MaxNoteScore); Score += score; updateAccuracyMessage(GetAccuracyValue(accuracy)); //destroy the note column.RemoveAt(ind); notesAttempted++; } else { //we are hitting nothing. bad //we dont actually care if the next note is too far away though if (dist < hitIgnoreDistance) { updateAccuracyMessage(GetAccuracyValue(-1)); Score -= MaxNoteScore; notesAttempted++; } } } for (int j = column.Count - 1; j >= 0; j--) { if (column[j] + NoteDestroyCushion < TimeElapsed) { column.RemoveAt(j); Score -= MaxNoteScore; notesAttempted++; } } } if (Randomized) { if (randomNoteCooldown > 0) { randomNoteCooldown -= delta; } else { randomNoteCooldown = randomNoteCooldownReset; //create a random note int column = GameMath.Choose(0, 1, 2, 3); float time = TimeElapsed + LookaheadTime; Columns[column].Add(time); Columns[column].Sort((a, b) => { return(Math.Sign(a - b)); }); } } if (TimeElapsed > TimeLength) { //game end Game.CurrentRoom = new EndRoom(Game, Parent, this); //intentionally make parent our parent so that we go back to map list } } if (timePositionFixCooldown > 0) { timePositionFixCooldown--; } else if (setMusicPositionToUsCooldown < 0) { if (hasSetMusicPositionToUs) { timePositionFixCooldown = timePositionFixCooldownReset; if ((TimeModifier == 0) != (!Music.isPlaying())) { if (TimeModifier == 0) { PauseMusic(); } else { PlayMusic((uint)TimeElapsed); } } if (Music.isPlaying()) { float timeDifference = Music.GetTotalMilliseconds() - TimeElapsed; if (Math.Abs(timeDifference) > timeEpsilon) { totalTimeDifference -= timeDifference; System.Diagnostics.Debug.WriteLine("total time difference: " + totalTimeDifference); } } } else { totalTimeDifference = 0; Music.SetTimePosition((uint)TimeElapsed); hasSetMusicPositionToUs = true; } } else { setMusicPositionToUsCooldown--; } if (totalTimeDifference != 0) { float amount = totalTimeDifference * totalTimeDifferenceScaleRate; totalTimeDifference -= amount; //TimeBegan += amount; } prevKeyPresses = keyPresses; }
//move ALL tiles simultaneously private void gravity_Tick(object sender, EventArgs e) { if (hitBar.Value == 0) { //initialize gameover GameOver(); } else { //detect beats detector.update(); if (localLastbeat != detector.getLastBeat()) { //spawn here Button btn = new Button(); myButtons butt = new myButtons(); btn.BackColor = butt.getBtnColor(); btn.FlatStyle = butt.getStyle(); btn.Size = butt.getSize(); btn.Font = butt.getFont(); btn.Location = butt.randLoc((rnd.Next(0, 4))); if (btn.Location == new Point(0, -22)) { btn.Text = "D"; } else if (btn.Location == new Point(71, -22)) { btn.Text = "F"; } else if (btn.Location == new Point(142, -22)) { btn.Text = "J"; } else if (btn.Location == new Point(213, -22)) { btn.Text = "K"; } Controls.Add(btn); btn.BringToFront(); //update last beat localLastbeat = detector.getLastBeat(); } //iterate through buttons foreach (Button item in Controls.OfType <Button>()) { if (item != start && item != loadSongs) { //check if item is beyond deleteLocation if (item.Top > deleteLocation) { //remove form controls Controls.Remove(item); //dispose button in array item.Dispose(); } else { //move tiles per tick item.Top += gamespeed; if (item.Top > maxHit && item.BackColor == Color.Black) { item.BackColor = Color.Red; item.Font = new Font("Segoe UI", 9, FontStyle.Bold); item.Text = "Missed!"; penalty = gamespeed * 10; DeductBar(penalty); //deduct hitbar value } } } } } //display score scoreDisplay.Text = "Score: " + Convert.ToString(score); //get high Score SetHighScore(); if (detector.isPlaying() == false) { GameOver(); } }