This repository has been archived by the owner on Jan 1, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
/
flxtileblock.monkey
146 lines (105 loc) · 2.79 KB
/
flxtileblock.monkey
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
Strict
Import mojo
Import flxsprite
Import flxg
Class FlxTileblock Extends FlxSprite Implements FlxSpriteRenderer
Global __CLASS__:Object
Private
Field _tilePixels:Image
Field _stamps:Stack<FlxStamp>
Public
Method New(x:Int, y:Int, width:Int, height:Int, makeGraphic:Bool = False)
Super.New(x, y)
If (makeGraphic) Then
MakeGraphic(width, height, 0)
Else
Pixels = Null
Self.width = width
Self.height = height
End If
SetRenderer(Self)
active = False
immovable = True
moves = False
_stamps = New Stack<FlxStamp>()
End Method
Method Destroy:Void()
If (_tilePixels <> Null) Then
_tilePixels = Null
End If
If (_stamps.Length() > 0) Then
Local i:Int = 0
Local l:Int = _stamps.Length()
Local stamp:FlxStamp
While (i < l)
_stamps.Set(i, Null)
i += 1
Wend
_stamps.Clear()
End If
Super.Destroy()
End Method
Method LoadTiles:FlxTileblock(tileGraphic:String, tileWidth:Int = 0, tileHeight:Int = 0, empties:Int = 0)
If (tileGraphic.Length() = 0) Return Self
Local sprite:FlxSprite = (New FlxSprite()).LoadGraphic(tileGraphic, True, False, tileWidth, tileHeight)
Local spriteWidth:Int = sprite.width
Local spriteHeight:Int = sprite.height
Local total:Int = sprite.frames + empties
_tilePixels = sprite.Pixels
If (width Mod spriteWidth <> 0) Then
width = Int(width / spriteWidth + 1) * spriteWidth
End If
If (height Mod spriteHeight <> 0) Then
height = Int(height / spriteHeight + 1) * spriteHeight
End If
If (_tilePixels <> Null) MakeGraphic(width, height, FlxG.WHITE)
Local row:Int = 0
Local column:Int
Local destinationX:Int
Local destinationY:Int = 0
Local widthInTiles:Int = width / spriteWidth
Local heightInTiles:Int = height / spriteHeight
Local i:Int = 0
_stamps.Clear()
While (row < heightInTiles)
destinationX = 0
column = 0
While (column < widthInTiles)
If (FlxG.Random() * total > empties) Then
_stamps.Push(New FlxStamp(destinationX, destinationY, Int(FlxG.Random() * (sprite.frames - 1))))
End If
destinationX += spriteWidth
column += 1
Wend
destinationY += spriteHeight
row += 1
Wend
Return Self
End Method
Method OnSpriteBind:Void(sprite:FlxSprite)
End Method
Method OnSpriteUnbind:Void()
End Method
Method OnSpriteRender:Void(x:Float, y:Float)
If (_tilePixels = Null) Return
Local i:Int = 0
Local l:Int = _stamps.Length()
Local stamp:FlxStamp
While (i < l)
stamp = _stamps.Get(i)
DrawImage(_tilePixels, x + stamp.x, y + stamp.y, stamp.frame)
i += 1
Wend
End Method
End Class
Private
Class FlxStamp
Field x:Int
Field y:Int
Field frame:Int
Method New(x:Int, y:Int, frame:Int)
Self.x = x
Self.y = y
Self.frame = frame
End Method
End Class