
:- dynamic(gui_haunter/2).

% Position 0 (away from village)
draw_haunter_ghost(Field,0) :-
	% remove old drawings
	gui_remove_haunter(Field),
	% draw everything
	haunt_position(Field, (Xactive,Yactive), (Xinactive, Yinactive)),
	file(haunter,Filename),
	file(haunter_inactive,InactiveFile),
	new(HaunterBitmap,bitmap(Filename)),
	new(InactiveBitmap,bitmap(InactiveFile)),
	send(@playground,display,HaunterBitmap,point(Xactive,Yactive)),
        send(@playground,display,InactiveBitmap,point(Xinactive,Yinactive)),
	assert(gui_haunter(Field,HaunterBitmap)),
	assert(gui_haunter(Field,InactiveBitmap)).

% Position 1 (facing the village)
draw_haunter_ghost(Field,1) :-
	% remove old drawings
	gui_remove_haunter(Field),
	% draw everything
	haunt_position(Field, (Xinactive,Yinactive), (Xactive, Yactive)),
	file(haunter,Filename),
	file(haunter_inactive,InactiveFile),
	new(HaunterBitmap,bitmap(Filename)),
	new(InactiveBitmap,bitmap(InactiveFile)),
	send(@playground,display,HaunterBitmap,point(Xactive,Yactive)),
        send(@playground,display,InactiveBitmap,point(Xinactive,Yinactive)),
	assert(gui_haunter(Field,HaunterBitmap)),
	assert(gui_haunter(Field,InactiveBitmap)).

gui_remove_all_haunters:-
        forall( gui_haunter(Field,_), gui_remove_haunter(Field)).
	
% dirty (das war rolf)
gui_remove_haunter(Ghostfield):-
	forall( gui_haunter(Ghostfield,Bitmap), 
		(free(Bitmap),retractall(gui_haunter(Ghostfield,Bitmap)))).
 
	
	



% haunt_position(Field, Position0, Position1 )
haunt_position(Field, Position0, Position1):-
	haunt_position( Field, OffsetType),
	haunt_offset( OffsetType, (X0, Y0), (X1,Y2)),
	tile(Field, Xpos, Ypos, _),
	Xpos0 is Xpos + X0,
	Ypos0 is Ypos + Y0,
	Xpos1 is Xpos + X1,
	Ypos1 is Ypos + Y2,
	Position0 = (Xpos0, Ypos0),
	Position1 = (Xpos1, Ypos1).
	
	


