From e295603a2f0a7d4a95f46b305d353badba618623 Mon Sep 17 00:00:00 2001 From: sekelsenmat Date: Thu, 17 Nov 2011 14:28:03 +0000 Subject: [PATCH] tappytux: Implements the hammer throwing git-svn-id: https://svn.code.sf.net/p/lazarus-ccr/svn@2155 8e941d3f-bd1b-0410-a28a-d453659cc2b4 --- applications/tappytux/gameconfigform.pas | 5 +- applications/tappytux/mod_tappymath.pas | 51 +++++++++++++----- applications/tappytux/mod_tappywords.pas | 67 ++++++++++++++++-------- applications/tappytux/tappydrawer.pas | 22 ++++++-- applications/tappytux/tappymodules.pas | 1 + applications/tappytux/tappytux.lpi | 1 + 6 files changed, 107 insertions(+), 40 deletions(-) diff --git a/applications/tappytux/gameconfigform.pas b/applications/tappytux/gameconfigform.pas index 808825dbc..d79e32055 100644 --- a/applications/tappytux/gameconfigform.pas +++ b/applications/tappytux/gameconfigform.pas @@ -67,7 +67,10 @@ begin labelWordlist.Caption := lModule.ConfigCaption; listWordlist.Items.Text := lModule.ConfigItems; if listWordlist.Items.Count >= comboLanguage.ItemIndex then - listWordlist.ItemIndex := comboLanguage.ItemIndex; + begin + if comboLanguage.ItemIndex < listWordlist.Items.Count then + listWordlist.ItemIndex := comboLanguage.ItemIndex; + end; end; procedure TformConfig.comboLanguageChange(Sender: TObject); diff --git a/applications/tappytux/mod_tappymath.pas b/applications/tappytux/mod_tappymath.pas index 8685b22f1..344c1a014 100644 --- a/applications/tappytux/mod_tappymath.pas +++ b/applications/tappytux/mod_tappymath.pas @@ -42,6 +42,7 @@ type procedure GameWon(); override; procedure GameLost(); override; procedure ProcessFallingTextEnd(); override; + procedure ProcessSpriteEnd(AUserData: TObject; APosition: TPoint); override; end; implementation @@ -260,8 +261,8 @@ procedure TTappyMath.Answered(AText: string); var i: Integer; j: Integer; - snowmanRight: TFallingText; lAnimation: TTappyTuxAnimation; + hammer: TTappySpriteAnimation; begin i:= 0; j:= vTappyTuxDrawer.GetAnimationCount - 1; @@ -276,18 +277,23 @@ begin gameLevel := (gameScore div 20) + gameSLevel; UpdateScore(gameScore); UpdateLevel(gameLevel); - snowmanRight := TFallingText.Create; - snowmanRight.IsInfinite := False; - snowmanRight.StartPoint := vTappyTuxDrawer.GetAnimation(i).Position; - snowmanRight.EndPoint := vTappyTuxDrawer.GetAnimation(i).Position; - snowmanRight.Position := vTappyTuxDrawer.GetAnimation(i).Position; - snowmanRight.StepCount := 2000; - snowmanRight.LoadImageFromPng(vTappyTuxConfig.GetResourcesDir() + 'images' + PathDelim + 'sprites' + PathDelim + 'snowmanright.png'); - snowmanRight.caption:= 'OK!'; - snowmanRight.ProcessOnEnd := False; - vTappyTuxDrawer.AddAnimation(snowmanRight); - vTappyTuxDrawer.RemoveAnimation(i); - i := i - 1; + + lAnimation.Stopped := True; + + hammer := TTappySpriteAnimation.Create; + hammer.IsInfinite := False; + hammer.StartPoint := Point(250, 328); + hammer.EndPoint := lAnimation.Position; + hammer.StepCount := 1000; + hammer.SpriteChangeInterval := 200; + SetLength(hammer.Images, 4); + hammer.LoadImageFromPng(0, vTappyTuxConfig.GetResourcesDir() + 'images' + PathDelim + 'sprites' + PathDelim + 'hammer_1.png'); + hammer.LoadImageFromPng(1, vTappyTuxConfig.GetResourcesDir() + 'images' + PathDelim + 'sprites' + PathDelim + 'hammer_2.png'); + hammer.LoadImageFromPng(2, vTappyTuxConfig.GetResourcesDir() + 'images' + PathDelim + 'sprites' + PathDelim + 'hammer_3.png'); + hammer.LoadImageFromPng(3, vTappyTuxConfig.GetResourcesDir() + 'images' + PathDelim + 'sprites' + PathDelim + 'hammer_4.png'); + hammer.UserData := lAnimation; + hammer.UserPosition := lAnimation.Position; + vTappyTuxDrawer.AddAnimation(hammer); end; end; i := i + 1; @@ -349,6 +355,25 @@ begin if gameLives <= 0 then GameLost(); end; +procedure TTappyMath.ProcessSpriteEnd(AUserData: TObject; APosition: TPoint); +var + snowmanRight: TFallingText; + lIndex: Integer; +begin + snowmanRight := TFallingText.Create; + snowmanRight.IsInfinite := False; + snowmanRight.StartPoint := APosition; + snowmanRight.EndPoint := APosition; + snowmanRight.Position := APosition; + snowmanRight.StepCount := 2000; + snowmanRight.LoadImageFromPng(vTappyTuxConfig.GetResourcesDir() + 'images' + PathDelim + 'sprites' + PathDelim + 'snowmanright.png'); + snowmanRight.caption:= 'OK!'; + snowmanRight.ProcessOnEnd := False; + vTappyTuxDrawer.AddAnimation(snowmanRight); + lIndex := vTappyTuxDrawer.GetAnimationIndex(TTappyTuxAnimation(AUserData)); + if lIndex >= 0 then vTappyTuxDrawer.RemoveAnimation(lIndex); +end; + initialization AddModule(TTappyMath.Create); end. diff --git a/applications/tappytux/mod_tappywords.pas b/applications/tappytux/mod_tappywords.pas index 5e542241b..b7eaeb172 100644 --- a/applications/tappytux/mod_tappywords.pas +++ b/applications/tappytux/mod_tappywords.pas @@ -46,6 +46,7 @@ type procedure GameWon(); override; procedure GameLost(); override; procedure ProcessFallingTextEnd(); override; + procedure ProcessSpriteEnd(AUserData: TObject; APosition: TPoint); override; end; var @@ -237,7 +238,7 @@ begin end; end; - snowmanAnimation.StartPoint := Point(xAux, 5); + snowmanAnimation.StartPoint := Point(xAux, -100); snowmanAnimation.EndPoint := Point(xAux, 270); snowmanAnimation.IsInfinite := false; snowmanAnimation.StepCount := GetFallingDurationFromLevel(); @@ -275,8 +276,8 @@ procedure TTappyWords.Answered(AText: string); var i: Integer; j: Integer; - snowmanRight: TFallingText; lAnimation: TTappyTuxAnimation; + hammer: TTappySpriteAnimation; begin i:= 0; j:= vTappyTuxDrawer.GetAnimationCount - 1; @@ -285,25 +286,30 @@ begin lAnimation := vTappyTuxDrawer.GetAnimation(i); if lAnimation is TFallingText then begin - if TFallingText(lAnimation).Caption = AText then - begin - gameScore := gameScore +1; - gameLevel := (gameScore div 20) + gameSLevel; - UpdateScore(gameScore); - UpdateLevel(gameLevel); - snowmanRight := TFallingText.Create; - snowmanRight.IsInfinite := False; - snowmanRight.StartPoint := vTappyTuxDrawer.GetAnimation(i).Position; - snowmanRight.EndPoint := vTappyTuxDrawer.GetAnimation(i).Position; - snowmanRight.Position := vTappyTuxDrawer.GetAnimation(i).Position; - snowmanRight.StepCount := 2000; - snowmanRight.LoadImageFromPng(vTappyTuxConfig.GetResourcesDir() + 'images' + PathDelim + 'sprites' + PathDelim + 'snowmanright.png'); - snowmanRight.caption:= 'OK!'; - snowmanRight.ProcessOnEnd := False; - vTappyTuxDrawer.AddAnimation(snowmanRight); - vTappyTuxDrawer.RemoveAnimation(i); - i := i - 1; - end; + if TFallingText(lAnimation).Caption = AText then + begin + gameScore := gameScore +1; + gameLevel := (gameScore div 20) + gameSLevel; + UpdateScore(gameScore); + UpdateLevel(gameLevel); + + lAnimation.Stopped := True; + + hammer := TTappySpriteAnimation.Create; + hammer.IsInfinite := False; + hammer.StartPoint := Point(250, 328); + hammer.EndPoint := lAnimation.Position; + hammer.StepCount := 1000; + hammer.SpriteChangeInterval := 200; + SetLength(hammer.Images, 4); + hammer.LoadImageFromPng(0, vTappyTuxConfig.GetResourcesDir() + 'images' + PathDelim + 'sprites' + PathDelim + 'hammer_1.png'); + hammer.LoadImageFromPng(1, vTappyTuxConfig.GetResourcesDir() + 'images' + PathDelim + 'sprites' + PathDelim + 'hammer_2.png'); + hammer.LoadImageFromPng(2, vTappyTuxConfig.GetResourcesDir() + 'images' + PathDelim + 'sprites' + PathDelim + 'hammer_3.png'); + hammer.LoadImageFromPng(3, vTappyTuxConfig.GetResourcesDir() + 'images' + PathDelim + 'sprites' + PathDelim + 'hammer_4.png'); + hammer.UserData := lAnimation; + hammer.UserPosition := lAnimation.Position; + vTappyTuxDrawer.AddAnimation(hammer); + end; end; i := i + 1; j := vTappyTuxDrawer.GetAnimationCount - 1; @@ -365,6 +371,25 @@ begin if gameLives <= 0 then GameLost(); end; +procedure TTappyWords.ProcessSpriteEnd(AUserData: TObject; APosition: TPoint); +var + snowmanRight: TFallingText; + lIndex: Integer; +begin + snowmanRight := TFallingText.Create; + snowmanRight.IsInfinite := False; + snowmanRight.StartPoint := APosition; + snowmanRight.EndPoint := APosition; + snowmanRight.Position := APosition; + snowmanRight.StepCount := 2000; + snowmanRight.LoadImageFromPng(vTappyTuxConfig.GetResourcesDir() + 'images' + PathDelim + 'sprites' + PathDelim + 'snowmanright.png'); + snowmanRight.caption:= 'OK!'; + snowmanRight.ProcessOnEnd := False; + vTappyTuxDrawer.AddAnimation(snowmanRight); + lIndex := vTappyTuxDrawer.GetAnimationIndex(TTappyTuxAnimation(AUserData)); + if lIndex >= 0 then vTappyTuxDrawer.RemoveAnimation(lIndex); +end; + initialization AddModule(TTappyWords.Create); end. diff --git a/applications/tappytux/tappydrawer.pas b/applications/tappytux/tappydrawer.pas index 2e4f05957..b72aaab08 100644 --- a/applications/tappytux/tappydrawer.pas +++ b/applications/tappytux/tappydrawer.pas @@ -20,6 +20,10 @@ type CurrentStep: Integer; StepCount: Integer; // In miliseconds IsInfinite: Boolean; // if True the animation will never end + Stopped: Boolean; + // User data + UserData: TObject; + UserPosition: TPoint; constructor Create; virtual; procedure DrawToIntfImg(AIntfImage: TLazIntfImage); virtual; procedure DrawToCanvas(ACanvas: TCanvas); virtual; @@ -80,6 +84,7 @@ type procedure HandleOnTimer(Sender: TObject); function AddAnimation(AAnimation: TTappyTuxAnimation): Integer; function GetAnimation(AIndex: Integer): TTappyTuxAnimation; + function GetAnimationIndex(AAnimation: TTappyTuxAnimation): Integer; function GetAnimationCount: Integer; procedure RemoveAnimation(AIndex: Integer); procedure HandleAnimationOnTimer(AInterval: Integer); @@ -102,8 +107,8 @@ destructor TTappySpriteAnimation.Destroy; var i: Integer; begin -{ for i := 0 to Length(Images)-1 do - if Assigned(Images[i]) then Images[i].Free;} + for i := 0 to Length(Images)-1 do + if Assigned(Images[i]) then Images[i].Free; inherited Destroy; end; @@ -122,7 +127,7 @@ end; procedure TTappySpriteAnimation.ExecuteFinal; begin - + GetCurrentModule().ProcessSpriteEnd(UserData, UserPosition); end; procedure TTappySpriteAnimation.LoadImageFromPng(AIndex: Integer; APath: string); @@ -428,6 +433,12 @@ begin Result := TTappyTuxAnimation(FAnimationList.Items[AIndex]); end; +function TTappyTuxDrawer.GetAnimationIndex(AAnimation: TTappyTuxAnimation + ): Integer; +begin + Result := FAnimationList.IndexOf(AAnimation); +end; + function TTappyTuxDrawer.GetAnimationCount: Integer; begin Result := FAnimationList.Count; @@ -447,8 +458,9 @@ begin while i < FAnimationList.Count do begin lAnimation := GetAnimation(i); - Inc(lAnimation.CurrentStep, AInterval); - if (not lAnimation.IsInfinite) and (lAnimation.CurrentStep >= lAnimation.StepCount) then + if (not lAnimation.Stopped) then Inc(lAnimation.CurrentStep, AInterval); + if (not lAnimation.IsInfinite) and (lAnimation.CurrentStep >= lAnimation.StepCount) + and (not lAnimation.Stopped) then begin lAnimation.ExecuteFinal(); RemoveAnimation(i); diff --git a/applications/tappytux/tappymodules.pas b/applications/tappytux/tappymodules.pas index 04cf7bffb..0a52c5f1f 100644 --- a/applications/tappytux/tappymodules.pas +++ b/applications/tappytux/tappymodules.pas @@ -39,6 +39,7 @@ type procedure GameWon(); virtual; abstract; procedure GameLost(); virtual; abstract; procedure ProcessFallingTextEnd(); virtual; abstract; + procedure ProcessSpriteEnd(AUserData: TObject; APosition: TPoint); virtual; abstract; end; procedure AddModule(AModule: TTappyModule); diff --git a/applications/tappytux/tappytux.lpi b/applications/tappytux/tappytux.lpi index 496a7c193..2103d4a1f 100644 --- a/applications/tappytux/tappytux.lpi +++ b/applications/tappytux/tappytux.lpi @@ -63,6 +63,7 @@ +