#p "under, above"
@map.cloning(@map.pieces[i])
xt=(-a/b*(s_rect.y-sy)-c/b).to_i
xtp=(-a/b*(s_rect.y-sy+s_rect.height)-c/b).to_i
yt=(-b/a*(s_rect.x-sx)-c/a).to_i
ytp=(-b/a*(s_rect.x-sx+s_rect.width)-c/a).to_i
@map.triangle(@map.pieces.last,sx+xt,s_rect.y,sx+xtp,s_rect.y+s_rect.height,sx+xtp,s_rect.y,true)
@map.triangle(@map.pieces[i],sx+xt,s_rect.y,sx+xtp,s_rect.y+s_rect.height,sx+xtp,s_rect.y)
@map.pieces.last.src_rect.set(s_rect.x,s_rect.y,s_rect.width,ytp-(s_rect.y-sy))
@map.pieces[i].y+=-s_rect.y+sy+yt
@map.pieces[i].src_rect.set(s_rect.x,sy+yt,s_rect.width,s_rect.y-sy+s_rect.height-yt)
end
end
end
else
dist1=(a*(s_rect.y-sy)+ b*(s_rect.x-sx) + c)/(a**2+b**2)**0.5
dist2=(a*(s_rect.y+s_rect.height-sy)+ b*(s_rect.x+s_rect.width-sx) + c)/(a**2+b**2)**0.5
square=(s_rect.height**2+s_rect.width**2)**0.5
#p (-b/a,-c/a,dist1,dist2,square,2)
if dist1.abs<square and dist2.abs<square and dist1*dist2<0
#p "slope pos"
if -b/a*(s_rect.x-sx)-c/a<(s_rect.y-sy+s_rect.height)
if -b/a*(s_rect.x-sx+s_rect.width)-c/a<(s_rect.y-sy)
#p "above, above"
@map.cloning(@map.pieces[i])
xt=(-a/b*(s_rect.y-sy)-c/b).to_i
yt=(-b/a*(s_rect.x-sx)-c/a).to_i
@map.triangle(@map.pieces.last,s_rect.x,yt+sy,s_rect.x,s_rect.y,xt+sx,s_rect.y,true)
@map.triangle(@map.pieces[i],s_rect.x,yt+sy,s_rect.x,s_rect.y,xt+sx,s_rect.y)
@map.pieces.last.src_rect.set(s_rect.x,s_rect.y,xt-(s_rect.x-sx),yt-(s_rect.y-sy))
else
#p "above, under"
@map.cloning(@map.pieces[i])
xt=(-a/b*(s_rect.y-sy)-c/b).to_i
xtp=(-a/b*(s_rect.y-sy+s_rect.height)-c/b).to_i
ytp=(-b/a*(s_rect.x-sx)-c/a).to_i
yt=(-b/a*(s_rect.x-sx+s_rect.width)-c/a).to_i
@map.triangle(@map.pieces.last,sx+xt,s_rect.y,sx+xtp,s_rect.y+s_rect.height,sx+xtp,s_rect.y,true)
@map.triangle(@map.pieces[i],sx+xt,s_rect.y,sx+xtp,s_rect.y+s_rect.height,sx+xtp,s_rect.y)
@map.pieces.last.src_rect.set(s_rect.x,s_rect.y,s_rect.width,ytp-(s_rect.y-sy))
@map.pieces[i].y+=-s_rect.y+sy+yt
@map.pieces[i].src_rect.set(s_rect.x,sy+yt,s_rect.width,s_rect.y-sy+s_rect.height-yt)
end
else
if -b/a*(s_rect.x-sx+s_rect.width)-c/a>(s_rect.y-sy)
#p "under, under"
@map.cloning(@map.pieces[i])
xt=(-a/b*(s_rect.y-sy+s_rect.height)-c/b).to_i
yt=(-b/a*(s_rect.x-sx+s_rect.width)-c/a).to_i
@map.triangle(@map.pieces.last,s_rect.x+s_rect.width,s_rect.y+s_rect.height,s_rect.x+s_rect.width,sy+yt,sx+xt,s_rect.y+s_rect.height,true)
@map.triangle(@map.pieces[i],s_rect.x+s_rect.width,s_rect.y+s_rect.height,s_rect.x+s_rect.width,sy+yt,sx+xt,s_rect.y+s_rect.height)
@map.pieces.last.src_rect.set(s_rect.x+xt,s_rect.y+yt,s_rect.x-sx+s_rect.width-xt,s_rect.y-sy+s_rect.height-yt)
@map.pieces.last.y-=-yt-sy+s_rect.y
@map.pieces.last.x-=s_rect.x-(sx+xt)
else
#p "under, above"
@map.cloning(@map.pieces[i])
xtp=(-a/b*(s_rect.y-sy)-c/b).to_i
xt=(-a/b*(s_rect.y-sy+s_rect.height)-c/b).to_i
yt=(-b/a*(s_rect.x-sx)-c/a).to_i
ytp=(-b/a*(s_rect.x-sx+s_rect.width)-c/a).to_i
@map.triangle(@map.pieces.last,s_rect.x,sy+yt,s_rect.x+s_rect.width,sy+ytp,s_rect.x,sy+ytp,true)
@map.triangle(@map.pieces[i],s_rect.x,sy+yt,s_rect.x+s_rect.width,sy+ytp,s_rect.x,sy+ytp)
@map.pieces.last.src_rect.set(s_rect.x,s_rect.y,xtp-(s_rect.x-sx),s_rect.height)
@map.pieces[i].x-=s_rect.x-(sx+xt)
@map.pieces[i].src_rect.set(sx+xt,s_rect.y,s_rect.x-sx+s_rect.width-xt,s_rect.height)
end
end
end
end
end
end
end
end
@map.pieces=@map.pieces.sort{|d,e| d.src_rect.y <=> e.src_rect.y}
Graphics.update
return [a,b,c]
else
p "DE error : No target selected"
end
end
end
def testing_sort#DO NOT USE IT
p @map.pieces.size
for i in
0..@map.pieces.size-1
@map.pieces[i].x=@bmpx+30*i+20
#p (1,@map.pieces[1].x)
#p (3,@map.pieces[3].x) if @map.pieces[3]!=nil
end
end
end
#==============================================================================
# ¦ DE_Map
# Manage pieces creation, cloning and modification
#==============================================================================
class DE_Map
attr_accessor :pieces
def initialize(target)
@target=target
@pieces=Array.new
@target.transparent=true
$DE_spriteset.update
@pieces[0]=DE_Piece.new($DE_viewport,@target,true)
Graphics.update
end
def cloning(to_dup)
@pieces.push(DE_Piece.new($DE_viewport,@target))
@pieces.last.bitmap=to_dup.bitmap.clone
@pieces.last.x=to_dup.x
@pieces.last.y=to_dup.y
@pieces.last.z=to_dup.z
@pieces.last.src_rect.x=to_dup.src_rect.x
@pieces.last.src_rect.y=to_dup.src_rect.y
@pieces.last.src_rect.width=to_dup.src_rect.width
@pieces.last.src_rect.height=to_dup.src_rect.height
end
def refresh_centre
for to_centre in pieces
ox=to_centre.ox
oy=to_centre.oy
to_centre.ox = to_centre.src_rect.width / 2
to_centre.oy = to_centre.src_rect.height
to_centre.x+=to_centre.ox-ox
to_centre.y+=(to_centre.oy-oy)
end
end
def collapse
for to_collapse in pieces
to_collapse.collapse if to_collapse.opacity!=0
end
for i in 1..48
for to_collapse in pieces
to_collapse.update
end
Graphics.update
end
end
def fadeout
for to_fadeout in pieces
to_fadeout.escape if to_fadeout.opacity!=0
end
for i in 1..48
for to_fadeout in pieces
to_fadeout.update
end
Graphics.update
end
end
#==============================================================================
# ? Draw a triangle which erase either what is inside or outside
#==============================================================================
def triangle(bmp = nil, x1 = 0, y1 = 0, x2 = 0, y2 = 0, x3 = 0, y3 = 0, invert = false)
if x1==x2 and y1==y2
# Fix an error : 2 points of the triangle are equal
triangle(bmp, x1, y1+1, x2, y2, x3, y3,invert)
return true
end
if x2==x3 and y2==y3
# Fix an error : 2 points of the triangle are equal
triangle(bmp, x1, y1, x2, y2+1, x3, y3,invert)
return true
end
if x1==x3 and y1==y3
# Fix an error : 2 points of the triangle are equal
triangle(bmp, x1, y1, x2, y2, x3, y3+1,invert)
return true
end
if Math.atan2( y1 - y2, x1-x2) - Math.atan2( y3 - y2, x3-x2) >0
# Fix an error : The triangle doesn't appear due to its convexity...
triangle(bmp, x1, y1, x3, y3, x2, y2,invert)
return true
end
color = bmp.bitmap.get_pixel(0, 0)
#==============================================================================
# ? Here's the rasterization algorithm (whatever this is ^^, but this one wasn't easy to find and adapt !)
#==============================================================================
# Deltas
dx12 = x1 - x2
dx23 = x2 - x3
dx31 = x3 - x1
dy12 = y1 - y2
dy23 = y2 - y3
dy31 = y3 - y1
# Bounding Rectangle
min_x = [x1,x2,x3].min
max_x = [x1,x2,x3].max
min_y = [y1,y2,y3].min
max_y = [y1,y2,y3].max
# Half-edge constants
c1 = dy12 * x1 - dx12 * y1
c2 = dy23 * x2 - dx23 * y2
c3 = dy31 * x3 - dx31 * y3
# Let's Calculate !
cy1 = c1 + dx12 * min_y - dy12 * min_x
cy2 = c2 + dx23 * min_y - dy23 * min_x
cy3 = c3 + dx31 * min_y - dy31 * min_x
for y_coord in min_y..max_y-1
cx1=cy1
cx2=cy2
cx3=cy3
for x_coord in min_x..max_x-1
if (cx1 > 0 and cx3 > 0 and cx2 > 0)!=invert
bmp.bitmap.set_pixel(x_coord, y_coord ,color)
end
cx1 -= dy12
cx2 -= dy23
cx3 -= dy31
end
cy1 += dx12
cy2 += dx23
cy3 += dx31
end
end
end
#==============================================================================
# ¦ DE_Piece
# This is the piece cut from the original sprite
#==============================================================================
class DE_Piece < RPG::Sprite
def initialize(viewport, character = nil,first=false)
super(viewport)
@character = character
if @tile_id != @character.tile_id or
@character_name != @character.character_name or
@character_hue != @character.character_hue
@tile_id = @character.tile_id
@character_name = @character.character_name
@character_hue = @character.character_hue
if @tile_id >= 384
self.bitmap = RPG::Cache.tile($game_map.tileset_name,
@tile_id, @character.character_hue)
self.src_rect.set(0, 0, 32, 32)
self.ox = 16
self.oy = 32
else
self.bitmap = RPG::Cache.character(@character.character_name,@character.character_hue) if !first
self.bitmap = RPG::Cache.character(@character.character_name,@character.character_hue).clone if first
@character.go_through if first
@cw = bitmap.width / 4
@ch = bitmap.height / 4
self.ox = @cw / 2
self.oy = @ch
sx = @character.pattern * @cw
sy = (@character.direction - 2) / 2 * @ch
self.src_rect.set(sx, sy, @cw, @ch)
end
end
@x=$game_map.display_x
@y=$game_map.display_y
self.x = @character.screen_x
self.y = @character.screen_y
self.z = @character.screen_z(bitmap.height/4)
self.opacity = @character.opacity
self.blend_type = @character.blend_type
self.bush_depth = @character.bush_depth
update
end
def update
super
self.x+=(@x-$game_map.display_x)/4
self.y+=(@y-$game_map.display_y)/4
@x=$game_map.display_x
@y=$game_map.display_y
self.z = @character.screen_z(bitmap.height/4)
end
end
#==============================================================================
# ¦ Linebmp
# Adds the ability to draw lines. Original by XRSX, adapted heavily to fit the DE
#==============================================================================
class Linebmp
attr_accessor :bmp
def initialize(x=0,y=0,z=0,w=32,h=48)
@w=w
@h=h
@bmp = Sprite.new($DE_viewport)
@bmp.x=x
@bmp.y=y
@bmp.z=z
@bmp.bitmap = Bitmap.new(@w,@h)
end
def erase
@bmp.bitmap = Bitmap.new(@w,@h)
end
def draw_line(start_x, start_y, end_x, end_y,speed,width,color=Color.new(255, 255, 255))
count=0
distance = (start_x - end_x).abs + (start_y - end_y).abs
for i in 1..distance
x = (start_x + 1.0 * (end_x - start_x) * i / distance).to_i
y = (start_y + 1.0 * (end_y - start_y) * i / distance).to_i
if !$DE.hide_line
if width == 1
@bmp.bitmap.set_pixel(x, y, color)
else
@bmp.bitmap.fill_rect(x, y, width, width, color)
end
end
count+=1
if count>=speed
sleep 0.01
Graphics.update
count=0
end
end
end
end
#===================================================
# ? CLASS Sprite_Character edit
#===================================================
class Sprite_Character < RPG::Sprite
alias de_initialize initialize
def initialize(viewport, character)
#character.reappear
if character.is_a?(Game_Player)
$DE_viewport=viewport
end
de_initialize(viewport, character)
end
end
#===================================================
# ? CLASS Game_Character edit
#===================================================
class Game_Character
alias de_Game_Character_initialize initialize
def initialize
de_Game_Character_initialize
@transparentde=@transparent
@throughde=@through
end
alias de_Game_Character_update update
def go_through
@through=true
end
def reappear
@transparent=@transparentde
@through=@throughde
end
def dont_go_through
@through=false
end
end
#===================================================
# ? CLASS Spriteset_Map edit
#===================================================
class Spriteset_Map
alias de_spriteset_update update
alias de_spriteset_initialize initialize
def initialize
$DE = DE.new
de_spriteset_initialize
$DE_spriteset = self
end
def update
$DE.update if $DE.is_a?(Destruction_Engine)
de_spriteset_update
end
end