Class: GGLib::Tile
- Inherits:
-
Object
- Object
- GGLib::Tile
- Defined in:
- lib/tile.rb
Overview
The Tile class represents a rectangular region on the screen. It is synonymous with the Rect structure used in many C programs.
Direct Known Subclasses
Constant Summary collapse
Instance Attribute Summary collapse
-
#id ⇒ Object
@@tile = [].
-
#inclusive ⇒ Object
@@tile = [].
-
#x1 ⇒ Object
@@tile = [].
-
#x2 ⇒ Object
@@tile = [].
-
#y1 ⇒ Object
@@tile = [].
-
#y2 ⇒ Object
@@tile = [].
Class Method Summary collapse
-
.deleteAllInstances ⇒ Object
Delete all tiles int the program.
-
.deleteById(id) ⇒ Object
Deletes the tile with the given index.
-
.getAllInstances ⇒ Object
Returns an array of all tiles.
-
.getById(id) ⇒ Object
Returns the tile with the given index.
-
.intersect?(tile1, tile2) ⇒ Boolean
Find out if the given two tiles intersect.
-
.setAllInstances(ntiles) ⇒ Object
Override the internal array of tiles with a new one.
Instance Method Summary collapse
-
#centerOn(x, y) ⇒ Object
translate the cordinates of the tile so that the center of the tile is at the given point.
-
#del ⇒ Object
Deletes the calling tile.
-
#each ⇒ Object
Iterates over all of the encompased by the tile.
-
#eachBorder(xindent = 0, yindent = 0) ⇒ Object
Iterates over all of the points on the edges of the tile.
-
#height ⇒ Object
Returns the width of the tile.
-
#initialize(x1 = 0, y1 = 0, x2 = 1, y2 = 1, inclusive = true, id = nil) ⇒ Tile
constructor
A new instance of Tile.
-
#intersect?(tile) ⇒ Boolean
See Tile::intersect?.
-
#isInTile?(x, y) ⇒ Boolean
(also: #contains?)
Find out if a point is located in the tile.
-
#iTile(x, y) ⇒ Object
Find out if a point is located in the tile, including the edges of the tile.
-
#move(x, y = @y) ⇒ Object
Translate the coordinates of the tile so that the thop left corner of the tile is at the given coordinates.
-
#resize(w, h) ⇒ Object
Resize the dimensions of the tile to be w by h units.
-
#setCoordinates(x1, y1, x2, y2) ⇒ Object
Redefine the tile.
-
#setTile(x1, y1, x2, y2) ⇒ Object
Redefine the tile.
-
#width ⇒ Object
Returns the hieght of the tile.
-
#xTile(x, y) ⇒ Object
Find out if a point is located in the tile, not including the edges of the tile.
Constructor Details
#initialize(x1 = 0, y1 = 0, x2 = 1, y2 = 1, inclusive = true, id = nil) ⇒ Tile
Returns a new instance of Tile.
10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 |
# File 'lib/tile.rb', line 10 def initialize(x1=0,y1=0,x2=1,y2=1,inclusive=true,id=nil) #id parameter is for use by saveload ONLY!!!! @x1,@y1,@x2,@y2=x1,y1,x2,y2 #begin # @x1+=0;@y1+=0;@x2+=0;@y2+=0 #check if coordinate is actually a proc (damn dynamic typing...) #rescue # raise "Bad coord: ID#{@@tiles.size+1}" #end @inclusive=inclusive #@name=name if id==nil @id=@@tiles.size #volatile, id overwritten by derived classes @tileno=@id #secure copy of id @@tiles << self else @id=id @tileno=id @@tiles[id]=self end end |
Instance Attribute Details
#id ⇒ Object
@@tile = []
8 9 10 |
# File 'lib/tile.rb', line 8 def id @id end |
#inclusive ⇒ Object
@@tile = []
8 9 10 |
# File 'lib/tile.rb', line 8 def inclusive @inclusive end |
#x1 ⇒ Object
@@tile = []
8 9 10 |
# File 'lib/tile.rb', line 8 def x1 @x1 end |
#x2 ⇒ Object
@@tile = []
8 9 10 |
# File 'lib/tile.rb', line 8 def x2 @x2 end |
#y1 ⇒ Object
@@tile = []
8 9 10 |
# File 'lib/tile.rb', line 8 def y1 @y1 end |
#y2 ⇒ Object
@@tile = []
8 9 10 |
# File 'lib/tile.rb', line 8 def y2 @y2 end |
Class Method Details
.deleteAllInstances ⇒ Object
Delete all tiles int the program. (Includes classes derived from tiles)
165 166 167 |
# File 'lib/tile.rb', line 165 def Tile.deleteAllInstances @@tiles=CArray.new end |
.deleteById(id) ⇒ Object
Deletes the tile with the given index
144 145 146 147 148 149 150 151 152 |
# File 'lib/tile.rb', line 144 def Tile.deleteById(id) if @@tiles[id]!=nil @@tiles[id].del @@tiles.delete_at(id) return true else return false end end |
.getAllInstances ⇒ Object
Returns an array of all tiles
155 156 157 |
# File 'lib/tile.rb', line 155 def Tile.getAllInstances return @@tiles end |
.getById(id) ⇒ Object
Returns the tile with the given index
135 136 137 138 139 140 141 |
# File 'lib/tile.rb', line 135 def Tile.getById(id) if @@tiles[id]!=nil return @@tiles[id] else return false end end |
.intersect?(tile1, tile2) ⇒ Boolean
Find out if the given two tiles intersect.
130 131 132 |
# File 'lib/tile.rb', line 130 def Tile.intersect?(tile1,tile2) return ((tile2.x1 < tile1.x2) and (tile2.x2 > tile1.x1) and (tile2.y1 < tile1.y2) and (tile2.y2 > tile1.y1)) end |
.setAllInstances(ntiles) ⇒ Object
Override the internal array of tiles with a new one
160 161 162 |
# File 'lib/tile.rb', line 160 def Tile.setAllInstances(ntiles) @@tiles=ntiles end |
Instance Method Details
#centerOn(x, y) ⇒ Object
translate the cordinates of the tile so that the center of the tile is at the given point
60 61 62 63 64 65 66 67 |
# File 'lib/tile.rb', line 60 def centerOn(x, y) hwidth=((@x2-@x1)/2).ceil.abs hheight=((@y2-@y1)/2).ceil.abs @x2=x+hwidth @y2=y+hheight @y1=y-hheight @x1=x-hwidth end |
#del ⇒ Object
Deletes the calling tile
170 171 172 173 |
# File 'lib/tile.rb', line 170 def del @@tiles.delete_at(@tileno) @x1,@y1,@x2,@y2,@inclusive,@id,@tileno=nil end |
#each ⇒ Object
Iterates over all of the encompased by the tile
96 97 98 99 100 101 102 |
# File 'lib/tile.rb', line 96 def each @x1.upto(@x2) { |x| @y1.upto(@y2) { |y| yield x,y } } end |
#eachBorder(xindent = 0, yindent = 0) ⇒ Object
Iterates over all of the points on the edges of the tile
105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 |
# File 'lib/tile.rb', line 105 def eachBorder(xindent=0,=0) (@x1+xindent).upto(@x2-xindent) { |x| yield x, @y1+ } (@x1+xindent).upto(@x2-xindent) { |x| yield x, @y2+ } (@y1+xindent).upto(@y2-xindent) { |y| yield @x1+xindent, y } (@y1+xindent).upto(@y2-xindent) { |y| yield @x2+xindent, y } end |
#height ⇒ Object
Returns the width of the tile
70 71 72 |
# File 'lib/tile.rb', line 70 def height return (@y2-@y1).abs end |
#intersect?(tile) ⇒ Boolean
See Tile::intersect?
125 126 127 |
# File 'lib/tile.rb', line 125 def intersect?(tile) return Tile::intersect?(self,tile) end |
#isInTile?(x, y) ⇒ Boolean Also known as: contains?
Find out if a point is located in the tile
31 32 33 34 35 36 37 |
# File 'lib/tile.rb', line 31 def isInTile?(x,y) if @inclusive return iTile(x,y) else return xTile(x,y) end end |
#iTile(x, y) ⇒ Object
Find out if a point is located in the tile, including the edges of the tile
86 87 88 |
# File 'lib/tile.rb', line 86 def iTile(x,y) return (x>=@x1 and x<=@x2 and y>=@y1 and y<=@y2) end |
#move(x, y = @y) ⇒ Object
Translate the coordinates of the tile so that the thop left corner of the tile is at the given coordinates
52 53 54 55 56 57 |
# File 'lib/tile.rb', line 52 def move(x,y=@y) @x2=x+(@x2-@x1) @y2=y+(@y2-@y1) @y1=y @x1=x end |
#resize(w, h) ⇒ Object
Resize the dimensions of the tile to be w by h units
80 81 82 83 |
# File 'lib/tile.rb', line 80 def resize(w, h) @x2 = @x1 + w @y2 = @y1 + h end |
#setCoordinates(x1, y1, x2, y2) ⇒ Object
Redefine the tile
42 43 44 |
# File 'lib/tile.rb', line 42 def setCoordinates(x1,y1,x2,y2) @x1,@y1,@x2,@y2=x1,y1,x2,y2 end |
#setTile(x1, y1, x2, y2) ⇒ Object
Redefine the tile
47 48 49 |
# File 'lib/tile.rb', line 47 def setTile(x1,y1,x2,y2) @x1,@y1,@x2,@y2=x1,y1,x2,y2 end |
#width ⇒ Object
Returns the hieght of the tile
75 76 77 |
# File 'lib/tile.rb', line 75 def width return (@x2-@x1).abs end |
#xTile(x, y) ⇒ Object
Find out if a point is located in the tile, not including the edges of the tile
91 92 93 |
# File 'lib/tile.rb', line 91 def xTile(x,y) return (x>@x1 and x<@x2 and y>@y1 and y<@y2) end |