summaryrefslogtreecommitdiff
path: root/game.bmx
blob: cc5a8b95a0394295d03f2b1cb4e8fcfd9a8e84a0 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
' Particle Pinch
'
' Copyright 2005 Ian Cowburn
'
' $Id$
'
Strict
Import noddybox.vector
Import noddybox.bitmapfont
Import "types.bmx"
Import "level.bmx"

Const LEVEL_NOTOVER:Int=	0
Const LEVEL_WON:Int=		1
Const LEVEL_LOST:Int=		2

Type TGame

	Field level:TLevel
	Field mass:TList
	Field point:TList
	Field timer:Int
	Field num:Int
	Field lost:Int
	Field captured:Int
	Field done:Int
	Field frame:Int
	Field placed:Int
	Field txtoff:Int[]
	Field playing:Int
	Field col:Int
	Field coli:Int
	
	Function Create:TGame(level:TLevel)
		Local o:TGame=New TGame
		
		o.level=level
		o.mass=CreateList()
		o.point=CreateList()
		o.level.CreatePlayfield(o.mass,o.point)
		o.timer=o.level.timer
		o.num=o.point.Count()
		o.done=LEVEL_NOTOVER
		o.frame=0
		o.placed=0
		o.txtoff=[GameGFX.font.TextWidth("PARTICLES"),GameGFX.font.TextWidth("CAPTURED"),GameGFX.font.TextWidth("LOST"),GameGFX.font.TextWidth("TIMER")]
		o.playing=False
		o.col=0
		o.coli=1
		Return o
	End Function
	
	Method Intro()
		Local y:Int=GraphicsHeight()/4
		Local yi:Int=25
		col:+coli
		
		If col=255 Or col=128
			coli=-coli
		EndIf
		
		SetScale(2,2)
	
		GameGFX.font.CentreColoured(level.name,y,col,col,255-col)
		y:+yi
	
		GameGFX.font.CentreColoured("Need "+level.winpercent+"% to clear",GraphicsHeight()/2,col/2,col,col)
		y:+yi

		GameGFX.font.CentreColoured("You can place "+level.maxmass+" masses",GraphicsHeight()/2,col/2,col,col)
		y:+yi

		If level.invmass	
			GameGFX.font.CentreColoured("PLACED MASSES ARE INVERTED!",y,col,col/2,col/2)
			y:+yi
		EndIf
	
		SetScale(1,1)
	End Method
	
	Method Play:Int()
		captured=0
		lost=0
		
		For Local m:TMass=EachIn mass
			For Local s:TPoint=EachIn point
				s.Attract(m)
			Next
		Next
		
		TParticleMachine.Process()
		
		For Local m:TMass=EachIn mass
			m.MoveAndDraw()
		Next
		
		For Local s:TPoint=EachIn point
			s.MoveAndDraw()
			
			If s.dead
				captured:+1
			ElseIf s.lost
				lost:+1
			EndIf
		Next
		
		frame:+1
		
		If frame=60 And timer>0
			timer:-1
		EndIf
		
		Local percent:Int=Int(Float(captured)/Float(num)*100.0)
		
		If (timer=0 Or num=0) And done=LEVEL_NOTOVER
			If percent>=level.winpercent
				done=LEVEL_WON
			Else
				done=LEVEL_LOST
			EndIf
		EndIf
		
		GameGFX.font.Draw("PARTICLES",0,0)
		GameGFX.font.DrawColoured(num-captured-lost,txtoff[0]+10,0,255,255,0)
		GameGFX.font.Draw("CAPTURED",200,0)
		GameGFX.font.DrawColoured(percent+"%",txtoff[1]+210,0,255,0,255)
		GameGFX.font.Draw("LOST",400,0)
		GameGFX.font.DrawColoured((100-percent)+"%",txtoff[2]+410,0,255,0,0)
		
		GameGFX.font.Draw("TIMER",600,0)
		
		If timer>10
			GameGFX.font.Draw(timer,txtoff[3]+610,0)
		Else
			GameGFX.font.DrawColoured(timer,txtoff[3]+610,0,255,0,0)
		EndIf
		
		Select done
			Case LEVEL_NOTOVER
				If playing
					If placed<level.maxmass And mass.Count()<MAX_GRAV
						If MouseHit(1)
							Local m:TMass=New TMass
							m.friend=False
							m.inverse=level.invmass
							m.x=MouseX()
							m.y=MouseY()
							m.img=GameGFX.mass
							mass.AddLast(m)
						EndIf
					EndIf
				Else
					Intro()
					If MouseHit(1)
						playing=True
					EndIf
				EndIf
			Case LEVEL_WON
				SetScale(2,2)
				GameGFX.font.CentreColoured("LEVEL COMPLETED!",GraphicsHeight()/2,255,255,0)
				SetScale(1,1)
			Case LEVEL_LOST
				SetScale(2,2)
				GameGFX.font.CentreColoured("LEVEL FAILED!",GraphicsHeight()/2,255,64,64)
				SetScale(1,1)
		EndSelect
		
		Return done
	End Method	

End Type