/* all drawing predicates for the main.pl go here 
*/

/************************* Basic drawing **************************************/
/* The following predicates are used for (more or less) basic drawing actions */
/* that can be connected to each other to gain some more complex drawing      */
/******************************************************************************/

/**
        @descr Draws all 4 player figures in the village field  where 
	       they belong. The position of a player can be changed 
	       by ->placeFigure/2
        @form  placeFigures
	*/
drawFigures:-
	drawFigure(@figYellow,yellow),
	drawFigure(@figRed,red),
	drawFigure(@figGreen,green),
	drawFigure(@figBlue,blue).

/**
	@descr Draws a certain player-figure on the village field where
	       it is placed according to player_figure
	@form  placeFigure(Figure,Color)
	       Figure: Handler to xpce-object
	       Color:  The color of the player (acts as identifier for player
		       as well as the color the figure is drawn in)
	       */
drawFigure(_,Color):-
	taoist_death(Color,_,_,_,_),
	!.

drawFigure(Figure,Color):-
	free(Figure),
	new(Figure, ellipse(20,20)),
	player_position(Color,Position),
	send(Figure,fill_pattern,colour(Color)),
	tile(Position,X,Y,v),
	taoist_offset(Color,XOffset,YOffset),
	Xpos is X + XOffset,
	Ypos is Y + YOffset,
	send(@playground,display,Figure,point(Xpos,Ypos)).

/**
	@descr Draws boxes around a list of field to highlight them.
               The list must contain the IDs of the fields that should be
	       highlighted. This works for village-fields (Number 1-9) as 
	       well as for ghost-fields (identified by a tuple (A,B))
        @form  highlightFields(FieldList)
	*/
highlightFields(FieldList):-
	forall((tile(ID,_,_,_),member(ID,FieldList)),(
	       highlightField(ID))).

highlightField(Field):-
	tile(Field,X,Y,v),
	drawBox(120,120,X,Y).

	
highlightField(Field):-
	tile(Field,X,Y,updown),
	drawBox(120,180,X,Y).


highlightField(Field):-
	tile(Field,X,Y,rightside),
	drawBox(180,120,X,Y).

highlightField(Field):-
	tile(Field,X,Y,leftside),
	drawBox(180,120,X,Y).

drawBox(Width,Height,Xpos,Ypos):-
	send(@playground,display,new(Box,box(Width,Height)),point(Xpos,Ypos)),
	assert(highlight(Box)),
	send(Box,colour,white),
	send(Box,pen,3).

/**
	@descr removes all highlights that where set by highlightFields/2 
	@form  nohighlight
	*/
nohighlight:-
	forall(highlight(Box),(free(Box),retract(highlight(Box)))).


/********************** More complex drawing **********************************/
/* The following predicates draw ghosts and villagefields in a more abstract  */
/* way than the simple drawing predicates above.                              */
/******************************************************************************/

show_preview_ghost(GhostID):-
	remove_preview_ghost,
	file(ghostcard,GhostID,updown,GhostFile),
	new(MonsterBitmap,bitmap(GhostFile)),
	retractall(ghost(preview,_)),
	assert(ghost(preview,MonsterBitmap)),
	send(@playground,display,MonsterBitmap,point(22,1)).

remove_preview_ghost:-
	gui_remove_ghost(preview).

% TODO: The following two predicates are more place_bitmap than place_ghost
% or place_villagefield

%place_ghost(+Position X, +PositionY)
place_ghost(GhostID,X,Y):-	
	remove_preview_ghost,
	gui_remove_ghost((X,Y)),
	tile((X,Y),Xpos,Ypos,Orientation),
	file(ghostcard,GhostID,Orientation,GhostFile),
	new(MonsterBitmap,bitmap(GhostFile)),
	%retractall(ghost((X,Y),_)),
	assert(ghost((X,Y),MonsterBitmap)),
	send(@playground,display,MonsterBitmap,point(Xpos,Ypos)).


% place_villagefield(+Fieldtype,+Position X, +Position Y)
place_villagefield(Fieldtype,FieldID):-
	!,
	file(villagefield,Fieldtype,normal,FilePath),
	tile(FieldID,X,Y,v),
	new(VillageFieldBitmap,bitmap(FilePath)),
	send(@playground,display,VillageFieldBitmap,point(X,Y)).


/**
	@descr Move a certain player-figure to a specified position
	       and draws all player-figures again.
        @form  moveFigure(Color,Position) 
	*/
moveFigure(Color,Position):-
	retractall(player_position(Color,_)),
	assert(player_position(Color,Position)),
	drawFigures.



