KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > buchuki > ensmer > builtin > Teleport


1 /*
2  * Copyright 2004 Dusty Phillips
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16
17 package com.buchuki.ensmer.builtin;
18
19 import com.buchuki.ensmer.*;
20 import com.buchuki.ensmer.input.event.semantic.*;
21 import com.buchuki.ensmer.object.Backend;
22 import java.io.Serializable JavaDoc;
23 import javax.vecmath.*;
24
25 /**
26  * Class to represent a teleport to a specific object. Maintains the identifier
27  * of the object and provides methods to execute the actual teleport.
28  * @author Dusty Phillips [dusty@buchuki.com]
29  */

30 public class Teleport extends Backend {
31     
32     /**
33      * Creates a new instance of a default Teleport. At this point, no object
34      * is associated with the teleport so setTeleportObject() should be called
35      * immediately.
36      */

37     public Teleport() {
38         final AreaManager man = EnsmerManager.instance().getAreaManager();
39         man.addObjectListener(new ObjectListener() {
40             public void objectStateChanged(ObjectEvent event) {
41                 if (event.getObject() == teleportIdentifier &&
42                     event.getAction() == ObjectEvent.EventType.OBJECT_DESTROYED) {
43                         man.getArea(man.getAreaIDForObject(getId())).destroyObject(getId());
44                 }
45             }
46         });
47     }
48     
49     /**
50      * Construct a teleport based on previously serialized data representing
51      * the identifier for the object to be teleported to.
52      * @param data the serialized data to rebuild a teleport from
53      */

54     public Teleport(Serializable JavaDoc data) {
55         this();
56         teleportIdentifier = (Long JavaDoc) data;
57     }
58     
59     /**
60      * Set the identifier of the object to teleport to
61      *
62      * @param id the identifier of the object that should be teleported
63      * to
64      */

65     public void setTeleportObject(Long JavaDoc id) {
66         this.teleportIdentifier = id;
67         fireChangeEvent();
68     }
69     
70     /**
71      * Get a reference to the teleport object's identifier
72      *
73      * @return the identifier of the object that should be teleported to
74      */

75     public Long JavaDoc getTeleportObject(Long JavaDoc id) {
76         return teleportIdentifier;
77     }
78     
79     /**
80      * Get the Serializable object for the backend
81      *
82      * @return a long object identifier
83      */

84     public Serializable JavaDoc getSerializable() {
85         return teleportIdentifier;
86     }
87     
88     /**
89      * Teleport to the object associated with the teleport
90      */

91     public void doTeleport() {
92         AreaManager man = EnsmerManager.instance().getAreaManager();
93         Area area = man.getArea(man.getAreaIDForObject(teleportIdentifier));
94         if (area != man.getCurrentArea()) {
95             man.setCurrentArea(area.getAreaID());
96         }
97         Matrix4f objectPos = area.getObjectPosition(teleportIdentifier);
98         Matrix4f userPos = EnsmerManager.instance().getInterfaceManager().getViewPosition();
99         Vector3f addition = new Vector3f(0f, 0f, 3f);
100         userPos.transform(addition);
101         Vector3f objPosVec = new Vector3f();
102         objectPos.get(objPosVec);
103         objPosVec.add(addition);
104         userPos.setTranslation(objPosVec);
105         EnsmerManager.instance().getUserManager().setAbsoluteUserPosition(userPos);
106         EnsmerManager.instance().getUserManager().getInputManager().systemMode();
107     }
108     
109     /**
110      * The identifier of the object to teleport to.
111      */

112     private Long JavaDoc teleportIdentifier;
113     
114 }
115
Popular Tags