Sunday, May 31, 2009

Second Life Written Project

Second Life Written Project

GAME & ANIMATION

Title: Jedi Master – Darth Vader

Teacher: Dr. 梅興




Made By:
Robin 林坤建
495511648
資工三甲
Outline
1. Introduction to Second Life
2. Introduction to Star Wars
3. Connections between SL – SW (Background)
4. How to make it done (Scripting lightsabers)
5. Introductions to Economy in Second Life
6. How to make it done (Scripting the force)
7. Problems met within
8. What to expect in the future


1. Introduction to Second Life
Second Life (SL) is a virtual world developed by Linden Lab. It's almost like Maxis' The Sims, but with online architecture. Although SL can be categorized as an MMORPG, but it's obviously quite different from the likes of WoW, Lineage, etc. There's no leveling inside the SL world. What's most important in the second life concept is to give the users (residents) a second chance to build their lives.

Residents in SL have their own avatar. They can create their own things (or just buy them with the SL world's currency). SL used 3D dimensional modeling tools that were built around simple geometric shapes. If you aware of software like 3dsMax or MAYA, you could also import them to the SL world.

Nowadays, there are branded companies such as IBM, CISCO, Adidas, Sony, Vodafone, etc that mostly share their new concepts in SL world.
To sum everything up, SL is not just a game, but it's a virtual world that could connect to the real world, and maybe in the future, it could change the way of living. Imagine, a CEO that needs to be in two meetings, by using SL, he could just send his avatar to substitute his body.



2. Introduction to Star Wars
Star Wars is an epic space opera franchise initially conceived by George Lucas. The first film in the franchise was simply titled Star Wars, but later had the subtitle Episode IV: A New Hope added to distinguish it from its sequels and prequels. It was originally released on May 25, 1977 by 20th Century Fox, and became a worldwide pop culture phenomenon, spawning two immediate sequels released in three-year intervals. Sixteen years after the release of the trilogy's final film, the first in a new prequel trilogy of films was released, again released in three-year intervals, with the final film released on May 19, 2005.
As of 2008, the overall box office revenue generated by the six Star Wars films has totalled approximately $4.3 billion, making it the third-highest grossing film series, behind only the James Bond and Harry Potter films.
Darth Vader(達斯•維德) who is the third biggest villain movie of all time was a hero before. Lightsaber(光劍) and The Force(原力) was introduced in star wars.


3. Connections between SL – SW (Background)
The Idea is to make something cool between our real life fantasies to be implemented inside our second life (popular culture inside a game). I made homework about lightsabers, so I’m thinking to include it inside the project. Children dream come true… :D

4. How to make it done (Scripting lightsabers)
This script is for the Hilt of Lightsaber. so it could be turned on or off if it's clicked. If the lightsaber is worn, than it will automatically be worn on the right hand. The idea is to throw the part of lightsaber if it's on or off.


integer myswitch;

default
{
state_entry()
{

myswitch=FALSE;
llRequestPermissions( llGetOwner(), PERMISSION_ATTACH );
}

run_time_permissions( integer vBitPermissions )
{
if (PERMISSION_ATTACH & vBitPermissions)
{
llAttachToAvatar( ATTACH_RHAND );
}
else
{
llOwnerSay( "Permission to attach denied" );
}
}

on_rez(integer rez)
{
if(!llGetAttached())
{ //reset the script if it's not attached.
llResetScript();
}
}

attach(key AvatarKey)
{
if(AvatarKey)
{//event is called on both attach and detach, but Key is only valid on attach
integer test = llGetAttached();
if (test) {
llOwnerSay( "May the force be with you" );
} else {
llOwnerSay( "The force is not with you yet");
}
}
}

touch_start(integer total_number)
{
llSetStatus(STATUS_PHYSICS, TRUE);

if(myswitch==FALSE)
{
//Turn saber ON
llMessageLinked(LINK_ALL_CHILDREN, 0, "start", NULL_KEY);
myswitch=TRUE;
}
else
{
//Turn saber OFF
llMessageLinked(LINK_ALL_CHILDREN, 0, "stop", NULL_KEY);
myswitch=FALSE;
}
}
}


This here is for the lightsaber to play animations on key press. Inside the object, there is choke, stand, slash, backslash and simple slash animation. There're also the sound of opening and closing sound of lightsaber. The llSetPrimitiveParams is used for the lights of the lightsaber. Start when it's on, stop when it's off. We also need permission for keypress and playanimation.

default
{
state_entry()
{
llSetPrimitiveParams([PRIM_FULLBRIGHT,ALL_SIDES,FALSE]);
}

link_message(integer sender_num, integer num, string str, key id)
{
if(str=="stop")
{
llPlaySound("88a4ff4e-12c7-5679-e349-1a443b333495", 1.0);
llSetTexture("8dcd4a48-2d37-4909-9f78-f7a9eb4ef903", ALL_SIDES);
llSetPrimitiveParams([PRIM_FULLBRIGHT,ALL_SIDES,FALSE]);
llSay(0, "Force-OFF.");
llReleaseControls();
llStopAnimation("stand");
}
if(str=="start")
{
llPlaySound("4366aeb3-4cbc-2362-0088-68aace7ede28", 1.0);
llSetTexture("59facb66-4a72-40a2-815c-7d9b42c56f60", ALL_SIDES);
llSetPrimitiveParams([PRIM_FULLBRIGHT,ALL_SIDES,TRUE]);
llSay(0, "Force-ON");
llRequestPermissions(llGetOwner(), PERMISSION_TAKE_CONTROLS | PERMISSION_TRIGGER_ANIMATION);
llStartAnimation("stand");
}
}

run_time_permissions(integer perm)
{
if (perm & PERMISSION_TAKE_CONTROLS)
{
llTakeControls(CONTROL_ROT_RIGHT | CONTROL_ROT_LEFT | CONTROL_FWD | CONTROL_BACK, TRUE, FALSE);
}
}

control(key id, integer level, integer edge)
{
integer just_pressed = level & edge;

if (just_pressed & CONTROL_ROT_RIGHT)
{
llSay(0,"slash");
llStopAnimation("slash");
llStartAnimation("simple_slash");
}
else if (just_pressed & CONTROL_ROT_LEFT)
{
llSay(0,"stab");
llStartAnimation("slash");
}
else if (just_pressed & CONTROL_FWD)
{
llSay(0,"back slash");
llStartAnimation("backslash");
}
else if (just_pressed & CONTROL_BACK)
{
llSay(0,"Choke Releaser");
llStopAnimation("choke");
}
}
}



5. Introductions to Economy in Second Life
Second Life has its own currency, which is called Linden Dollar (L$). L$ can be used as a mean of tradings inside the SL world. There‘s also a currency exchange available for L$ as in the First Life (Real World), which is 250L$ for 1USD. 壹元台幣大概吧捌~玖元L$ .


6. How to make it done (Scripting the force)
This here is to make the force on other avatar clicked on the mask. Inside the object, there is choke and forceSTART animation. The idea here is to make a controller of other avatar, like the teleporter. But it's in the mask. we use runtime permission to make it happen.

reset()
{
llRequestPermissions(llDetectedKey(0), PERMISSION_TRIGGER_ANIMATION);
}

default
{
state_entry()
{
llSetText("DON'T TOUCH DARTH VADER's MASK", <1,1,1>, 1);
}

touch_start(integer startup_param)
{
reset();
}
run_time_permissions(integer perm)
{
if (perm & PERMISSION_TRIGGER_ANIMATION)
{
llStartAnimation("choke");
llSay(0,"choked");
llStartAnimation("forceSTART");
}
else
{
llSay(0,"The force is strong with you, But you can't fight Darth Vader!!!");
llStartAnimation("choke");
llStartAnimation("forceSTART");
}
}
}

7. Problems met within
a) The script for the force need to be activated by the user itself
b) We can’t throw parameters to the other script
c) Free animation editor is limited
d) Second life scripting code examples are mostly not open source
e) Limited Money

8. What to expect in the future
a) The real Force
b) Better lightsabers
c) Double Lightsaber (connected)
d) Better animations
e) Force Lightning
f) HUD usage
g) Weapons can be used in the jedi-arena

for the DEMO on youtube, go to part1, part2, and part3

AJAX

AJAX (asynchronous JavaScript and XML), is a group of interrelated web development techniques used on the client-side to create interactive web applications or rich Internet applications. With Ajax, web applications can retrieve data from the server asynchronously in the background without interfering with the display and behavior of the existing page. The use of Ajax has led to an increase in interactive animation on web pages and better quality of Web services thanks to the asynchronous mode. Data is retrieved using the XMLHttpRequest object. Despite the name, the use of JavaScript and XML is not actually required, nor do the requests need to be asynchronous.

Tutorial:
1. Put this inside <head>

<script type="text/javascript">
var xmlhttp;

function loadXMLDoc(url)
{
xmlhttp=null;
if (window.XMLHttpRequest)
{// code for IE7, Firefox, Mozilla, etc.
xmlhttp=new XMLHttpRequest();
}
else if (window.ActiveXObject)
{// code for IE5, IE6
xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
}
if (xmlhttp!=null)
{
xmlhttp.onreadystatechange=onResponse;
xmlhttp.open("GET",url,true);
xmlhttp.send(null);
}
else
{
alert("Your browser does not support XMLHTTP.");
}
}

function onResponse()
{
if(xmlhttp.readyState!=4) return;
if(xmlhttp.status!=200)
{
alert("Problem retrieving XML data");
return;
}

txt="<table border='1'>";
x=xmlhttp.responseXML.documentElement.getElementsByTagName("event");
for (i=0;i<x.length;i++)
{
txt=txt + "<tr>";
xx=x[i].getElementsByTagName("img");
{
try
{
txt=txt + "<td><img src=" + xx[0].firstChild.nodeValue + ">";
}
catch (er)
{
txt=txt + "<td> </td>";
}
}
xx=x[i].getElementsByTagName("title");
{
try
{
txt=txt + "<td>" + xx[0].firstChild.nodeValue + "</td>";
}
catch (er)
{
txt=txt + "<td> </td>";
}
}
xx=x[i].getElementsByTagName("address");
{
try
{
txt=txt + "<td>" + xx[0].firstChild.nodeValue + "</td>";
}
catch (er)
{
txt=txt + "<td> </td>";
}
}
txt=txt + "</tr>";
}
txt=txt + "</table>";
document.getElementById('copy').innerHTML=txt;
}

</script>


2. Put this inside <body>
<div id="copy">
<button onclick="loadXMLDoc('data.xml')">Get data</button>
</div>


3. Here's xml sample data

<?xml version="1.0" encoding="utf-8"?>
<events>
<event>
<title>aaa</title>
<address>aaaaa</address>
<img>http://www.vendian.org/howbig/UnstableURLs/letter_a.gif</img>
<content>aaaaaaa</content>
</event>

<event>
<title>bbbbb</title>
<address>bbbbbbbb</address>
<img>http://www.daniellesplace.com/Images4/letterB.gif</img>
<content>bbbbbbbb</content>
</event>
</events>


Explanation:
1. This function is for loading the XMLHttp

function loadXMLDoc(url)
{
xmlhttp=null;
if (window.XMLHttpRequest)
{// code for IE7, Firefox, Mozilla, etc.
xmlhttp=new XMLHttpRequest();
}
else if (window.ActiveXObject)
{// code for IE5, IE6
xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
}
if (xmlhttp!=null)
{
xmlhttp.onreadystatechange=onResponse;
xmlhttp.open("GET",url,true);
xmlhttp.send(null);
}
else
{
alert("Your browser does not support XMLHTTP.");
}
}


2. If the above statement didn't return null, then the onResponse function will run in the background

function onResponse()
{
if(xmlhttp.readyState!=4) return;
if(xmlhttp.status!=200)
{
alert("Problem retrieving XML data");
return;
}

this here makes the column of each tag found on XML data

txt="<table border='1'>";
x=xmlhttp.responseXML.documentElement.getElementsByTagName("event");
for (i=0;i<x.length;i++)
{
txt=txt + "<tr>";
xx=x[i].getElementsByTagName("img");
{
try
{
txt=txt + "<td><img src=" + xx[0].firstChild.nodeValue + ">";
}
catch (er)
{
txt=txt + "<td> </td>";
}
}
xx=x[i].getElementsByTagName("title");
{
try
{
txt=txt + "<td>" + xx[0].firstChild.nodeValue + "</td>";
}
catch (er)
{
txt=txt + "<td> </td>";
}
}
xx=x[i].getElementsByTagName("address");
{
try
{
txt=txt + "<td>" + xx[0].firstChild.nodeValue + "</td>";
}
catch (er)
{
txt=txt + "<td> </td>";
}
}
txt=txt + "</tr>";
}
txt=txt + "</table>";

Here, the document get the "copy" id of div in <body>

document.getElementById('copy').innerHTML=txt;
}


3. The id is used to get the innerHTML in onResponse function. On button click, the page will show the data from the XML

<div id="copy">
<button onclick="loadXMLDoc('data.xml')">Get data</button>
</div>

4. The code on onResponse function will only get "event" tag's <title>, <address>, and <img>. The <content> will be passed

<?xml version="1.0" encoding="utf-8"?>
<events>
<event>
<title>aaa</title>
<address>aaaaa</address>
<img>http://www.vendian.org/howbig/UnstableURLs/letter_a.gif</img>
<content>aaaaaaa</content>
</event>

<event>
<title>bbbbb</title>
<address>bbbbbbbb</address>
<img>http://www.daniellesplace.com/Images4/letterB.gif</img>
<content>bbbbbbbb</content>
</event>
</events>

For more sources, go to:
1. http://www.xul.fr/en-xml-ajax.html
2. http://www.w3schools.com/Ajax/Default.Asp
3. http://www.dmoz.org/Computers/Programming/Languages/JavaScript/AJAX/

Youtube API

The YouTube Application Programming Interface, or the YouTube API, allows developers to access video statistics and YouTube channels' data via two types of calls, REST and XML-RPC.

In order to use Youtube's API, a developer must first acquire a unique Developer ID — it's an additional property that upon reception, is attached to the developer's YouTube account. The information that is available to developers is similar to the information that can be acquired by accessing YouTube's many RSS feeds.

As of March 2006, API calls from Flash have been disabled due to security concerns.

Tutorial:
This time I'll let you embed the code first, and then I'll explain what will the code do


1. Put this inside <head>

<script src="http://www.google.com/jsapi"></script>
<script>
google.load("swfobject", "2.1");
</script>

<script type="text/javascript">
function onYouTubePlayerReady(playerId) {
ytplayer = document.getElementById("myytplayer");
updateytplayerInfo();
ytplayer.addEventListener("onError");
}

// functions for the api calls
function loadNewVideo() {
if (ytplayer) {
ytplayer.loadVideoById('iLaTDP3j3sw','0');
}
}

function play() {
if (ytplayer) {
ytplayer.playVideo();
}
}

function pause() {
if (ytplayer) {
ytplayer.pauseVideo();
}
}

function stop() {
if (ytplayer) {
ytplayer.stopVideo();
}
}


</script>


2. Put this into the body

<body onClick = "loadNewVideo()" >
<table width="760" border="0" align="center" cellpadding="0" cellspacing="0" bordercolor="#ECE9D8" >
<tr>
<td align="center"><div>
<!-- embed the player -->

<div id="ytapiplayer">
You need Flash player 8+ and JavaScript enabled to view this video.
</div>
<script type="text/javascript" >
var params = { allowScriptAccess: "always", bgcolor: "#cccccc" };
var atts = { id: "myytplayer" };
swfobject.embedSWF("http://www.youtube.com/apiplayer?enablejsapi=1&playerapiid=ytplayer",
"ytapiplayer", "760", "480", "8", null, null, params, atts);
</script>
<br />

</div>
</div>
</td>
</tr>
<tr>
<td align="center">Press Anywhere to PLAY / REPLAY or</td>
</tr>
</table>
</body>


Explanation:
1. Here is the usage of google ajax libraries API and the loader of SWFObject

<script src="http://www.google.com/jsapi"></script>
<script>
google.load("swfobject", "2.1");
</script>


2. Declaration that the script uses javascript


<script type="text/javascript">
...
</script>




3. When the player is ready, It will load the myytplayer


function onYouTubePlayerReady(playerId) {
ytplayer = document.getElementById("myytplayer");
updateytplayerInfo();
ytplayer.addEventListener("onError");
}


4. Call the source of the video

function loadNewVideo() {
if (ytplayer) {
ytplayer.loadVideoById('iLaTDP3j3sw','0');
}
}


5. Play the loaded video

function play() {
if (ytplayer) {
ytplayer.playVideo();
}
}


6. Pause the loaded video


function pause() {
if (ytplayer) {
ytplayer.pauseVideo();
}
}


7. Stop the loaded video

function stop() {
if (ytplayer) {
ytplayer.stopVideo();
}
}


8. When the body is clicked, it will load the video and play it

<body onClick = "loadNewVideo()" >
...
</body>


9. The var params allows access to use script. The var atts sets the id of the object or embed tag to 'myytplayer'. The last line is to embed the player inside the website

<script type="text/javascript" >
var params = { allowScriptAccess: "always", bgcolor: "#cccccc" };
var atts = { id: "myytplayer" };
swfobject.embedSWF("http://www.youtube.com/apiplayer?enablejsapi=1&playerapiid=ytplayer",
"ytapiplayer", "760", "480", "8", null, null, params, atts);
</script>
For more info, you could go to :
1. http://code.google.com/apis/youtube/getting_started.html
2. http://code.google.com/apis/youtube/js_api_reference.html
3. http://code.google.com/apis/youtube/chromeless_example_1.html

Adobe Flex + google maps with flash API

Adobe Flex is a software development kit released by Adobe Systems for the development and deployment of cross-platform rich Internet applications based on the Adobe Flash platform. Flex applications can be written using Adobe Flex Builder or by using the freely available Flex compiler from Adobe. Flex is used to make a SWF file, without using adobe flash.

Tutorial
A. Setting the environment
1. Download Adobe Flex SDK from here
2. Unzip the SDK into any directory (C:\test\flexSDK3 in this tutorial)
3. Try whether the SDK is already set. From command prompt, run C:\test\flexSDK3\bin\mxmlc If it succeeds, the command prompt should show

Adobe Flex Compiler (mxmlc)
Version 3.0.0 build 477
Copyright (c) 2004-2007 Adobe Systems, Inc. All rights reserved.
...


4. For easier use, add the computer path C:\test\flexSDK3\bin. From control panel --> system --> advanced(tabs) --> Environment Variables --> path

B. Collaborating with google maps
1. Download google maps with flash API here
2. Unzip the docs folder inside C:\test\flexSDK3
3. Unzip the lib folder's file inside C:\test\flexSDK3\frameworks\libs\
4. Make a folder in C:\test called src
5. Make a file inside the src folder, called HelloMap.mxml
6. Put this code

<?xml version="1.0" encoding="utf-8"?>
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute">
<maps:Map xmlns:maps="com.google.maps.*" id="map" mapevent_mapready="onMapReady(event)"
width="100%" height="100%" key="ABQIAAAABloZD40AKeJkN2oNklEhmRT2yXp_ZAY8_ufC3CFXhHIE1NvwkxRmpwvOMLpnUPIHmzDieHJ-t68QqA"/>
<mx:Script>
<![CDATA[

import com.google.maps.LatLng;
import com.google.maps.Map;
import com.google.maps.MapEvent;
import com.google.maps.MapType;

private function onMapReady(event:Event):void {
this.map.setCenter(new LatLng(40.736072,-73.992062), 14, MapType.NORMAL_MAP_TYPE);
}
]]>
</mx:Script>
</mx:Application>



7. Compile the mxml code. Go to command prompt, write "mxml C:\test\src\HelloMap.mxml"

8. VOILA! you have a swf file that's made without adobe flash.