Class: GGLib::Tile

Inherits:
Object
  • Object
show all
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

Containable

Constant Summary collapse

@@tiles =
CArray.new

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

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

#idObject

@@tile = []



8
9
10
# File 'lib/tile.rb', line 8

def id
  @id
end

#inclusiveObject

@@tile = []



8
9
10
# File 'lib/tile.rb', line 8

def inclusive
  @inclusive
end

#x1Object

@@tile = []



8
9
10
# File 'lib/tile.rb', line 8

def x1
  @x1
end

#x2Object

@@tile = []



8
9
10
# File 'lib/tile.rb', line 8

def x2
  @x2
end

#y1Object

@@tile = []



8
9
10
# File 'lib/tile.rb', line 8

def y1
  @y1
end

#y2Object

@@tile = []



8
9
10
# File 'lib/tile.rb', line 8

def y2
  @y2
end

Class Method Details

.deleteAllInstancesObject

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

.getAllInstancesObject

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.

Returns:

  • (Boolean)


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

#delObject

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

#eachObject

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,yindent=0)
  (@x1+xindent).upto(@x2-xindent) { |x|
    yield x, @y1+yindent
  }
  
  (@x1+xindent).upto(@x2-xindent) { |x|
    yield x, @y2+yindent
  }
  
  (@y1+xindent).upto(@y2-xindent) { |y|
    yield @x1+xindent, y
  }
  
  (@y1+xindent).upto(@y2-xindent) { |y|
    yield @x2+xindent, y
  }
end

#heightObject

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?

Returns:

  • (Boolean)


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

Returns:

  • (Boolean)


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

#widthObject

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