summaryrefslogtreecommitdiff
path: root/sprite.bmx
blob: f146dd714552a61b04c13039cc10874e646ee34f (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
'
' Provides sprite wrappers routines
'
' $Id$
'
Strict

Import noddybox.vector

Type TSprite
	Field x:Float
	Field y:Float
	Field v:TVector 

	' These fields are private
	'	
	Field img:TImage
	Field frames:Int
	Field pong:Int
	Field fr:Int
	Field colmask:Int
	Field aninmove:Int
	Field animspeed:Int
	Field animframe:Int
	Field animframeinc:Int

	Function Create:TSprite(i:TImage,anim_speed:Int,ping_pong:Int,anim_with_delta:Int,col_mask:Int)
		Local s:TSprite=New TSprite
		
		s.img=i
		s.frames=i.frames.length-1
		s.fr=0
		s.colmask=col_mask
		s.animframe=0
		s.animframeinc=1
		s.pong=ping_pong
		s.aninmove=anim_with_delta
		s.animspeed=anim_speed
		
		s.v=TVector.Create(0,0,0)
		
		s.x=0
		s.y=0
		s.v.x=0
		s.v.y=0
		
		Return s
	End Function
	
	Method ChangeAnim(anim_speed:Int,ping_pong:Int,anim_with_delta:Int)
		pong=ping_pong
		aninmove=anim_with_delta
		animspeed=anim_speed
	End Method
	
	Method ChangeImage(i:TImage,col_mask)
		img=i
		frames=i.frames.length-1
		colmask=col_mask
		animframe=0
		animframeinc=1
	End Method
	
	Method Update()
		x:+v.x
		y:+v.y
		
		DrawImage(img,x,y,animframe)
		
		If (colmask<>0)
			CollideImage(img,x,y,animframe,0,colmask,Self)
		EndIf
		
		fr:+1
			
		If (frames>0 And ((Not aninmove) Or v.x<>0 Or v.y<>0))
		
			If (fr>animspeed)
				fr=0
				
				animframe:+animframeinc
			
				If (pong)
					If (animframe=0 Or animframe=frames)
						animframeinc=-animframeinc
					EndIf
				Else
					If (animframe>frames)
						animframe=0
					EndIf
				EndIf
			EndIf
		EndIf
	End Method
	
End Type