Commits

Philémon Gardet committed 24c4a8f Merge

Merge branch 'superNode' Crush !!!!

Comments (0)

Files changed (4)

game/rooms/begin/script.lua

 function main ()
 	give_item "duck"
 	place_item ("duck",428,456)
+	add_character ("kedu","lit",-60,10)
 	sayHello()
 	return 
 end
 			h <- int_of_string(idle#getAttr "h");
 			ox <- (try int_of_string(idle#getAttr "ox") with _ -> 0);
 			oy <- (try int_of_string(idle#getAttr "oy") with _ -> 0)
-		
+
 		(* Draw Moving *)
-		method getLine (g,h) (i,j) = 
-			let rec line (a,b) (x,y) = 
+		method getLine ?speed:(r=1) (g,h) (i,j) = 
+			let rec line i (a,b) (x,y) = 
 					match (a,b,x,y) with
 						(* diagonales *)
-						| (a,b,x,y) when (x > a)&&(y > b) -> 
+						| (a,b,x,y) when (x > a)&&(y > b) ->
+							begin
+							if (i mod r = 0) then 
 							push (a,b) (positionUpdate);
 							push 1 direction;
-							line (a + 1,b + 1) (x,y)
+							end;
+							line (i+1) (a + 1,b + 1) (x,y) 
 						| (a,b,x,y) when (x > a)&&(y < b) -> 
+							begin
+							if (i mod r = 0) then 
 							push (a,b) (positionUpdate);
 							push 3 direction;
-							line (a + 1,b - 1) (x,y)
+							end;
+							line (i+1) (a + 1,b - 1) (x,y)
 						| (a,b,x,y) when (x < a)&&(y < b) -> 
+							begin
+							if (i mod r = 0) then 
 							push (a,b) (positionUpdate);
 							push 5 direction;
-							line (a - 1,b - 1) (x,y)
+							end;
+							line (i+1) (a - 1,b - 1) (x,y)
 						| (a,b,x,y) when (x < a)&&(y > b) -> 
+							begin
+							if (i mod r = 0) then 
 							push (a,b) (positionUpdate);
 							push 7 direction;
-							line (a - 1,b + 1) (x,y)
+							end;
+							line (i+1) (a - 1,b + 1) (x,y)
 						(* hauteurs *)
 						| (a,b,x,y) when (x = a)&&(y > b) -> 
+							begin
+							if (i mod r = 0) then 
 							push (a,b) (positionUpdate);
 							push 0 direction;
-							line (a,b + 1) (x,y)
+							end;
+							line (i+1) (a,b + 1) (x,y)
 						| (a,b,x,y) when (x = a)&&(y < b) -> 
+							begin
+							if (i mod r = 0) then 
 							push (a,b) (positionUpdate);
 							push 4 direction;
-							line (a,b - 1) (x,y)
+							end;
+							line (i+1) (a,b - 1) (x,y)
 						| (a,b,x,y) when (x > a)&&(y = b) -> 
+							begin
+							if (i mod r = 0) then 
 							push (a,b) (positionUpdate);
 							push 2 direction;
-							line (a + 1,b) (x,y)
+							end;
+							line (i+1) (a + 1,b) (x,y)
 						| (a,b,x,y) when (x < a)&&(y = b) -> 
+							begin
+							if (i mod r = 0) then 
 							push (a,b) (positionUpdate);
 							push 6 direction;
-							line (a - 1,b) (x,y)
+							end;
+							line (i+1) (a - 1,b) (x,y)
 						| _ -> ()
 			
 			and func (a,b) (x,y) =
 			
 			let rec final (a,b) (x,y) =
 				match (a,b) with 
-				|(a,b) when (a = x)||(b = y) -> 
-					line (a,b) (x,y)
+				|(a,b) when ((a = x)||(b = y)) -> 
+					line 1 (a,b) (x,y)
 				|(a,b) when x > a ->
 					let c = a + 1 in
 					let d = f a in
-					line (a,b) (c,d);
+					line 1 (a,b) (c,d);
 					final (c,d) (x,y)
 				|(a,b) ->
 					let c = a - 1 in
 					let d = f a in
-					line (a,b) (c,d);
+					line 1 (a,b) (c,d);
 					final (c,d) (x,y)
 			in
 			final (g,h) (i,j)
 			
-		
 		(* Update Methods *)
 		method setAnimation name =
 			let noper n =
 			actualSurface
 		method getOffset =
 			(ox, oy)
-
+		
 	end
 ;;
 
 	}
 ;;
 
+type luaReturnType = 
+	| LInt of int
+	| LString of string
+;;
+
 (* Object *********************************************************************)
 (* ************************************************************************** *)
 class sdlWindow width height =
 			currentDialog <- Some (new dialog xmlDialog id)
 		method setAnimation objectName animationName =
 			(displayData#get objectName).updating#setAnimation animationName
-			
+		
+		(* Absolute Moving *)
 		method placeTo objectName newPosition =
 			(displayData#get objectName).pos <- newPosition
 		method moveTo objectName ?actual newPosition =
 			in
 			(displayData#get objectName).updating#getLine actualPosition newPosition
 		
-		method walkToNode characterName ?previousNode node =
+		(* Node Moving *)
+		method walkToNode characterName ?previousNode node () =
 			let beginNode = match previousNode with
 				| None -> 
 						let currentNode = self#getNodes#getCurrentNode
 					let pos = self#getNodes#getCoor n
 					in self#moveTo characterName ~actual:prevPos pos; browser pos q
 			in browser beginNode path
-		method walkToPos characterName pos =
+		method walkToPos characterName pos () =
 			let node = self#getNodes#getNearestNode pos
-			in if node <> "" then self#walkToNode characterName node
-			
+			in if node <> "" then self#walkToNode characterName node ()
+		
+		method placeOffsetNode objectName node (ox,oy) =
+			let (x,y) = self#getNodes#getCoor node in
+			self#placeTo objectName (x+ox,y+oy)
+		
+		method moveOffstNode objectName node (ox,oy) =
+			let (x,y) = self#getNodes#getCoor node in
+			self#moveTo objectName ~actual:(x,y) (x+ox,y+oy)
 			
+		(* Change Room *)
 		method changeRoom name beginNode () =
 			let _ =
 				self#setLoadingMode;
 			); 
 			self#addCharacterToDisplay player beginNode;
 			currentRuntime <- Some (Wally.newLua self#getRoom#getScript);
-			ignore (self#runFunction "main");
+			ignore (self#doFunction "main" ());
 			self#setGameMode
 		
 		(** Manager Mode **)
 				Sdlevent.enable_events Sdlevent.mousemotion_mask;
 				currentItem <- None;
 				currentMode <- "inventory";
+				self#updateGame;
 				self#invInitDisplay
 			end
 			
 		method private updateGame =
 			self#gameUpdataDisplayData;
 			self#gameDisplay;
+			self#gameDisplayItem;
 			self#gameDisplayDialog;
 		 
 		method private gameUpdataDisplayData =
 			in 
 			blit_surface ~src:background ~dst:(modes#get "game") ();
 			browser (displayData#elements ())
+		method private gameDisplayItem = match currentItem with
+			| None -> ()
+			| Some name -> 
+				let image = 
+					let item = (getItem name) in
+					load_image (item#getDir//item#getThumnail)
+				and (x,y,_) = Sdlmouse.get_state ()
+				in let dst_rect = rect x y 0 0
+				in blit_surface ~src:image ~dst:(modes#get "game") ~dst_rect ()
 		method private gameDisplayDialog = match currentDialog with
 			| None -> ()
 			| Some d when d#is_empty () -> currentDialog <- None
 						| KEY_i -> self#setInventoryMode
 						| _ -> self#updateEvents
 					end
+				| Sdlevent.MOUSEBUTTONDOWN b 
+					when b.Sdlevent.mbe_button = Sdlmouse.BUTTON_MIDDLE -> self#setInventoryMode
 				| Sdlevent.MOUSEBUTTONDOWN b when self#playerStill -> 
 					self#mouseClickUpdating b
 				| _ -> self#updateEvents
 			let x = b.Sdlevent.mbe_x
 			and y = b.Sdlevent.mbe_y
 			in match b.Sdlevent.mbe_button with
+					| Sdlmouse.BUTTON_LEFT -> 
+						let sObject = self#selectedObject (x,y)
+						in begin if sObject <> "" then
+							let action = function u -> 
+								ignore (self#doFunction ("active_"^sObject) 
+									~args:[LString (self#getCurrentItem)] u)
+							in
+							push (self#walkToPos player (x,y)) priorityFunc;
+							push (action) priorityFunc;
+							push (function () -> currentItem <- None) priorityFunc
+						end
 					| Sdlmouse.BUTTON_RIGHT -> 
 						let beforeNode = self#getNodes#getCurrentNode in
-						self#walkToPos player (x,y);
+						self#walkToPos player (x,y) ();
 						begin if beforeNode <> self#getNodes#getCurrentNode then 
 						match self#getNodes#needChangeRoom with
 							| None -> ()
 		method private inventoryInputUser = function
 		  | Sdlevent.KEYDOWN key -> 
 		  	begin match key.Sdlevent.keysym with
-					| KEY_i | KEY_ESCAPE -> self#setGameMode
+					| KEY_i | KEY_ESCAPE -> currentItem <- None; self#setGameMode
+					| _ -> self#updateEvents
+				end
+			| Sdlevent.MOUSEBUTTONDOWN b ->
+				begin match (b.Sdlevent.mbe_button,currentItem) with
+					| (Sdlmouse.BUTTON_MIDDLE,_) -> currentItem <- None; self#setGameMode
+					| (Sdlmouse.BUTTON_LEFT,Some name) -> self#setGameMode
 					| _ -> self#updateEvents
 				end
 			| Sdlevent.MOUSEMOTION info -> 
 				| _ -> failwith "Error : invalid state of programm"
 			end
 			
-		(** Low Level Functions **)
+		(** Low Level Display Functions **)
 		method private createAlphaSurface w h =
 			let pi = surface_format !window in
 			let r = pi.rmask
 				done
 			done;
 			left ()
-			
-		method private runFunction ?args:(arg=[]) name =
+		
+		(** Low Level Lua Functions **)
+		method private doFunction ?args:(arg=[]) name () =
 			let runtime = match currentRuntime with
 				| Some runtime -> runtime
 				| None -> failwith "Lua runtime is not initialized"
 			in
 			let arguments = 
-				let rec browser = function
+				let rec browser = 
+					let parseType = function
+						| LInt v -> string_of_int v
+						| LString v -> "\""^v^"\n"
+					in function
 					| [] -> ""
-					| a::[] -> a
-					| a::q -> a^","^(browser q)
+					| a::[] -> parseType a
+					| a::q -> (parseType a)^","^(browser q)
 				in browser arg 
 			in runtime#doLine (name^"("^arguments^")")
 		
+		(** Getter Functions **)
 		method private getNodes = match nodes with
 			| Some n -> (n:Wally.graph)
 			| None -> failwith "Pathfinding motor is not initialized"
+			
+		method private getCurrentItem = match currentItem with
+			| None -> ""
+			| Some name -> name
 		
 		method private selectedObject (hx,hy) =
 			let inHitBox elementName = 
 				| _::q -> browser q
 			in browser (displayData#keys ())
 		
-		method playerStill =
+		method private playerStill =
 			(displayData#get player).updating#still
-		
+			
 		(** Debug Method **)
 		method printDisplayedElement =
 			let rec mkstr = function 
 					invDropItem name
 			| _ -> failwith "Invalid call in lua at drop_item"
 	and addcharacter state =
-		match (Lua.tostring state 1),(Lua.tostring state 2)
+		match (Lua.tostring state 1),(Lua.tostring state 2),
+			(Lua.tointeger state 3),(Lua.tointeger state 4)
 		with
-			| (Some name,Some node) -> 
+			| (Some name,Some node,ox,oy) -> 
 				let w = Jed.getWindow () in 
-				w#addCharacterToDisplay name node
+				w#addCharacterToDisplay name node;
+				begin if (ox <> 0 || oy <> 0) then
+					w#placeOffsetNode name node (ox,oy)
+				end
 			| _ -> failwith "Invalid call in lua at add_character"
 	and placeitem state =
 		match (Lua.tostring state 1),(Lua.tointeger state 2),(Lua.tointeger state 3) 
 				| [] -> failwith "Impossible case."
 				| a :: [] -> d
 				| a :: b :: t -> 
-					print_string(b ^ " -> "^ a ^ "\n");
 					browser (
 					d +. (match (m.(self#getId a).(self#getId b)) with 
 						| Finite(x) -> x 
 			let track = browser [] (-1.) ways
 			in if track <> [] then
 				(currentNode <- dst;
-				print_string("Current: "^currentNode^"\n");
 				List.rev track)
 			else
 				[]