Rafael Gozlan avatar Rafael Gozlan committed 507b687 Merge

Merge branch 'superNode' of https://bitbucket.org/winsproject/wins into superNode

Comments (0)

Files changed (3)

game/rooms/begin/script.lua

 function main ()
 	give_item "duck"
 	place_item ("duck",428,456)
+	add_character ("kedu","lit",-60,10)
 	sayHello()
 	return 
 end

game/scripts/main.lua

 function sayHello ()
 	print_debug "hello"
-	add_character ("kedu","lit",-60,10)
 	return
 end
 	}
 ;;
 
+type luaReturnType = 
+	| LInt of int
+	| LString of string
+;;
+
 (* Object *********************************************************************)
 (* ************************************************************************** *)
 class sdlWindow width height =
 		
 		val mutable currentRuntime = None
 		val mutable nodes = None
-		val mutable envElem = []
 		
 		(* Inventory Mode *)
 		val mutable invBackground = 
 				Sdlevent.KEYDOWN_EVENT;
 				Sdlevent.MOUSEBUTTONDOWN_EVENT;
 				Sdlevent.QUIT_EVENT]
-			);
-			envElem <- displayData#elements ();
+			)
 		
 		(** Window Manager **)
 		method getSurface =
 			(displayData#get objectName).updating#getLine actualPosition newPosition
 		
 		(* Node Moving *)
-		method walkToNode characterName ?previousNode node =
+		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#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 =
-			let rec organize = function
-				| [] -> true
-				| a :: [] -> true
-				| a :: b :: t when (let (_,y) = a.pos and (_,z) = b.pos in ((z > y)||(z = y))) -> organize (b :: t)
-				| _ -> false
-			in
-			let rec reorganize e = function
-				| [] -> []
-				| a :: [] -> [a]
-				| a :: b :: t when ((b = e)&&(let (_,y) = a.pos and (_,z) = b.pos in z < y)) -> b :: a :: t
-				| a :: b :: t when ((a = e)&&(let (_,y) = a.pos and (_,z) = b.pos in z < y))-> b :: a :: t
-				| a :: t -> a :: reorganize e t
-			in
-			let rec test = function
-				| [] -> ()
-				| element :: t when element.updating#still -> while (organize envElem = false) do 
-																envElem <- reorganize element envElem 
-															  done
-				| _ :: t -> test t
-			in test envElem;
-
 			let rec browser = function
 				| [] -> ()
 				| element::q ->
 							| _ -> ()
 					end;
 					browser q
-			in browser (envElem)
-		method gameDisplay =
+			in browser (displayData#elements ())
+		method private gameDisplay = 
 			let rec browser = function
 				| [] -> ()
 				| element::q -> 
 			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
 			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.MOUSEBUTTONDOWN b 
-					when b.Sdlevent.mbe_button = Sdlmouse.BUTTON_MIDDLE -> self#setGameMode
 			| Sdlevent.MOUSEMOTION info -> 
 				self#updateInvText info.Sdlevent.mme_x info.Sdlevent.mme_y
 			| _ -> self#updateEvents
 				| _ -> 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 
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.