| 1 | ########################################################################## | ||
| 2 | # HIGH SCORE TABLE | ||
| 3 | ########################################################################## | ||
| 4 | package Games::PangZero::Highscore; | ||
| 5 | |||
| 6 | use vars qw( @Games::PangZero::UnsavedHighScores ); | ||
| 7 | |||
| 8 | foreach (@Games::PangZero::DifficultyLevels) { | ||
| 9 | $_->{highScoreTablePan} = [ ['UPI', 250000], ['UPI', 200000], ['UPI', 150000], ['UPI', 100000], ['UPI', 50000] ]; | ||
| 10 | $_->{highScoreTablePan} = [ ['UPI', 2500], ['UPI', 2000], ['UPI', 1500], ['UPI', 1000], ['UPI', 500] ] if $_->{name} eq 'Miki'; | ||
| 11 | $_->{highLevelTablePan} = [ ['UPI', 50], ['UPI', 40], ['UPI', 30], ['UPI', 20], ['UPI', 10] ]; | ||
| 12 | $_->{highLevelTablePan} = [ ['UPI', 20], ['UPI', 16], ['UPI', 12], ['UPI', 8], ['UPI', 4] ] if $_->{name} eq 'Miki'; | ||
| 13 | $_->{highScoreTableCha} = [ ['UPI', 250000], ['UPI', 200000], ['UPI', 150000], ['UPI', 100000], ['UPI', 50000] ]; | ||
| 14 | $_->{highLevelTableCha} = [ ['UPI', 30], ['UPI', 25], ['UPI', 20], ['UPI', 15], ['UPI', 10] ]; | ||
| 15 | } | ||
| 16 | |||
| 17 | sub AddHighScore { | ||
| 18 | my ($player, $score, $level) = @_; | ||
| 19 | |||
| 20 | unshift @Games::PangZero::UnsavedHighScores, [$player, $score, $level]; | ||
| 21 | } | ||
| 22 | |||
| 23 | sub MergeUnsavedHighScores { | ||
| 24 | my ($table) = @_; | ||
| 25 | my ($unsavedHighScore, $player, $score, $level); | ||
| 26 | |||
| 27 | die unless ($table =~ /^(Cha|Pan)$/); | ||
| 28 | foreach $unsavedHighScore (@Games::PangZero::UnsavedHighScores) { | ||
| 29 | ($player, $score, $level) = @{$unsavedHighScore}; | ||
| 30 | &MergeUnsavedHighScore( $Games::PangZero::DifficultyLevel->{"highScoreTable$table"}, $player, $score ); | ||
| 31 | &MergeUnsavedHighScore( $Games::PangZero::DifficultyLevel->{"highLevelTable$table"}, $player, $level ); | ||
| 32 | } | ||
| 33 | |||
| 34 | splice @{$Games::PangZero::DifficultyLevel->{"highScoreTable$table"}}, 5; | ||
| 35 | splice @{$Games::PangZero::DifficultyLevel->{"highLevelTable$table"}}, 5; | ||
| 36 | @Games::PangZero::UnsavedHighScores = (); | ||
| 37 | my $newHighScore = &InputPlayerNames($table); | ||
| 38 | if ($newHighScore) { | ||
| 39 | $Game->RunHighScore( $Games::PangZero::DifficultyLevelIndex, $table, 0 ); | ||
| 40 | } | ||
| 41 | } | ||
| 42 | |||
| 43 | sub MergeUnsavedHighScore { | ||
| 44 | my ($highScoreList, $player, $score) = @_; | ||
| 45 | my ($i); | ||
| 46 | |||
| 47 | for ($i = 0; $i < scalar @{$highScoreList}; ++$i) { | ||
| 48 | if ($highScoreList->[$i]->[1] < $score) { | ||
| 49 | splice @{$highScoreList}, $i, 0, [$player, $score]; | ||
| 50 | return; | ||
| 51 | } | ||
| 52 | } | ||
| 53 | } | ||
| 54 | |||
| 55 | sub InputPlayerNames { | ||
| 56 | my ($table) = @_; | ||
| 57 | my ($highScoreEntry, $player, $score, $message, $retval); | ||
| 58 | |||
| 59 | die unless ($table =~ /^(Cha|Pan)$/); | ||
| 60 | $retval = 0; | ||
| 61 | foreach $highScoreEntry (@{$Games::PangZero::DifficultyLevel->{"highScoreTable$table"}}, @{$Games::PangZero::DifficultyLevel->{"highLevelTable$table"}}) { | ||
| 62 | $player = $highScoreEntry->[0]; | ||
| 63 | next unless ref $player; | ||
| 64 | unless ($player->{highScoreName}) { | ||
| 65 | $score = $highScoreEntry->[1]; | ||
| 66 | $message = $score < 1000 ? "Level $score" : "Score $score"; | ||
| 67 | $player->{highScoreName} = &InputPlayerName($player, $message); | ||
| 68 | } | ||
| 69 | $highScoreEntry->[0] = $player->{highScoreName}; | ||
| 70 | $retval = 1; | ||
| 71 | } | ||
| 72 | foreach $player (@Games::PangZero::Players) { | ||
| 73 | delete $player->{highScoreName}; | ||
| 74 | } | ||
| 75 | return $retval; | ||
| 76 | } | ||
| 77 | |||
| 78 | sub InputPlayerName { | ||
| 79 | my ($player, $message) = @_; | ||
| 80 | my ($nameMenuItem, @menuItems, $x, $y, $yInc); | ||
| 81 | |||
| 82 | SDL::Events::enable_unicode(1); | ||
| 83 | $Games::PangZero::UnicodeMode = 1; | ||
| 84 | my $name = ($player->{name} or '') . '|'; | ||
| 85 | my $guy = Games::PangZero::Guy->new($player); | ||
| 86 | ($guy->{x}, $guy->{y}) = (150, 150); | ||
| 87 | $guy->DemoMode(); | ||
| 88 | |||
| 89 | ($x, $y, $yInc) = (230, 80, 45); | ||
| 90 | push @menuItems, ( | ||
| 91 | Games::PangZero::MenuItem->new( $x, $y += $yInc, "HIGH SCORE!!!"), | ||
| 92 | Games::PangZero::MenuItem->new( $x, $y += $yInc, $message), | ||
| 93 | Games::PangZero::MenuItem->new( $x, $y += $yInc, "Please enter your name:"), | ||
| 94 | $nameMenuItem = Games::PangZero::MenuItem->new( $x, $y += $yInc, $name ), | ||
| 95 | ); | ||
| 96 | push @Games::PangZero::GameObjects, ($guy, @menuItems); | ||
| 97 | |||
| 98 | while (1) { | ||
| 99 | $Games::PangZero::LastUnicodeKey = 0; | ||
| 100 | $Game->MenuAdvance(); | ||
| 101 | last if $Game->{abortgame}; | ||
| 102 | if (%Events) { | ||
| 103 | my ($key) = %Events; | ||
| 104 | if ($key == SDLK_BACKSPACE) { | ||
| 105 | substr($name, -2, 1, ''); # Remove next to last char | ||
| 106 | $nameMenuItem->SetText($name); | ||
| 107 | } elsif ($key == SDLK_RETURN) { | ||
| 108 | last; | ||
| 109 | } elsif ($LastUnicodeKey < 127 and $Games::PangZero::LastUnicodeKey >= 32 and length($name) < 9) { | ||
| 110 | substr($name, -1, 0, chr($Games::PangZero::LastUnicodeKey)); # Insert before last char | ||
| 111 | $nameMenuItem->SetText($name); | ||
| 112 | } | ||
| 113 | } | ||
| 114 | } | ||
| 115 | $name =~ s/\|$//; | ||
| 116 | $player->{name} = $name; | ||
| 117 | $name = "Anonymous" if $name =~ /^\s*$/; | ||
| 118 | $guy->Delete(); | ||
| 119 | foreach (@menuItems) { | ||
| 120 | $_->Delete(); | ||
| 121 | } | ||
| 122 | SDL::Events::enable_unicode(0); $Games::PangZero::UnicodeMode = 0; | ||
| 123 | return $name; | ||
| 124 | } | ||
| 125 | |||
| 126 | 1; |