001 package examples.multiview; 002 003 import java.awt.geom.Point2D; 004 005 import fang.*; 006 007 008 /**Demonstrates scrolling by moving the 009 * mouse in the direction desired from 010 * the center of the screen. This example 011 * extends on the Navigator example by 012 * adding extra players, each with their 013 * own view. 014 * @author Jam Jenkins*/ 015 public class MultiView extends GameLoop 016 { 017 /** tiles image reflected vertically and 018 * horizontally to make it seamless.*/ 019 private ImageSprite background; 020 021 /** the players*/ 022 private StringSprite[] playerSprites; 023 024 /** the trackers for the players*/ 025 private ProjectileTracker[] playerTrackers; 026 027 /**the tracker used for the scrolling*/ 028 private ScrollTracker tracker; 029 030 /** makes and adds the sprites */ 031 public void startGame() 032 { 033 makeSprites(); 034 addSprites(); 035 setHelp("resources/MultiViewHelp.txt"); 036 } 037 038 /** makes the image sprites for the background 039 * and sets a scrolling tracker on it. Also 040 * sets each player's sprite to have a 041 * ProjectileTracker on it.*/ 042 private void makeSprites() 043 { 044 playerSprites=new StringSprite[getNumberOfPlayers()]; 045 playerTrackers=new ProjectileTracker[getNumberOfPlayers()]; 046 for(int i=0; i<playerSprites.length; i++) 047 { 048 playerSprites[i]=new StringSprite(getPlayer(i).getName()); 049 playerSprites[i].setWidth(0.25); 050 playerSprites[i].setLocation(0.5, 0.5); 051 playerTrackers[i]=new ProjectileTracker(new Point2D.Double()); 052 playerSprites[i].setTracker(playerTrackers[i]); 053 } 054 055 background=new ImageSprite("resources/road.jpg"); 056 background.setScale(1); 057 tracker=new ScrollTracker( 058 true, 059 new Point2D.Double(), 060 background, 061 3); 062 background.setTracker(tracker); 063 } 064 065 /**adds the sprites to the canvas*/ 066 private void addSprites() 067 { 068 canvas.addSprite(background); 069 for(StringSprite sprite: playerSprites) 070 canvas.addSprite(sprite); 071 } 072 073 /** 074 * gets the velocity to move the player at. 075 * The player only moves on mouse click. If 076 * you want the player to move like in the 077 * Navigator example, change the first line 078 * to just get the mouse location instead of 079 * the mouse click location. 080 * @param index the index of the player's mouse 081 * @return a velocity equivalent to the vector 082 * from the mouse position to the center of the 083 * screen. 084 */ 085 private Point2D.Double getVelocity(int index) 086 { 087 Point2D.Double mouse=getPlayer(index).getMouse().getClickLocation(); 088 if(mouse==null) return new Point2D.Double(); 089 Point2D.Double center=new Point2D.Double(0.5, 0.5); 090 double distance=mouse.distance(center); 091 double maxDistance=center.distance(1, 1); 092 Point2D.Double v; 093 /**only move if more than 10% the distance from 094 * the center to the corner*/ 095 if(distance/maxDistance>0.1) 096 { 097 v=new Point2D.Double( 098 center.x-mouse.x, 099 center.y-mouse.y); 100 } 101 else 102 v=new Point2D.Double(); 103 return v; 104 } 105 106 /**uses the position of the mouse in relation 107 * to the center of the screen to move the 108 * background in the direction and speed 109 * indicated. The angle from the center to the 110 * mouse if the direction the background moves 111 * and as the distance from the center of the 112 * screen increases, so too does the scroll speed*/ 113 public void advanceFrame(double timeAdvanced) 114 { 115 Point2D.Double v=getVelocity(getID()); 116 for(int i=0; i<playerTrackers.length; i++) 117 { 118 Point2D.Double velocity=getVelocity(i); 119 if(i!=getID()) 120 { 121 //other players movements are affected not only 122 //by their own mouse, but also by the scrolling 123 //of this player 124 playerTrackers[i].setVelocity(new Point2D.Double( 125 v.x-velocity.x, v.y-velocity.y)); 126 } 127 else 128 { 129 //this player never moves. Instead, the other 130 //players and background scroll by. 131 tracker.setVelocity(new Point2D.Double( 132 velocity.x, velocity.y)); 133 } 134 } 135 136 } 137 138 /** 139 * sets up the game for 2 or more players 140 * @param argv not used. 141 */ 142 public static void main(String[] argv) 143 { 144 MultiView multiView=new MultiView(); 145 multiView.playersSelectable=true; 146 multiView.players=2; 147 multiView.runAsApplication(); 148 } 149 } |