Browse Source

Added parallax scrolling support & new example room.

master
chrisgregan 11 years ago
parent
commit
23f66b733e
  1. BIN
      Assets/Fungus/Prefabs/ParallaxSprite.prefab
  2. 4
      Assets/Fungus/Prefabs/ParallaxSprite.prefab.meta
  3. 8
      Assets/Fungus/Scripts/CameraController.cs
  4. 23
      Assets/Fungus/Scripts/Game.cs
  5. 60
      Assets/Fungus/Scripts/Parallax.cs
  6. 8
      Assets/Fungus/Scripts/Parallax.cs.meta
  7. BIN
      Assets/Fungus/Sprites/ButtonBlank.psd
  8. 45
      Assets/Fungus/Sprites/ButtonBlank.psd.meta
  9. BIN
      Assets/Fungus/Sprites/Mushroom.png
  10. 45
      Assets/Fungus/Sprites/Mushroom.png.meta
  11. BIN
      Assets/FungusExample/Scenes/Example.unity
  12. 7
      Assets/FungusExample/Scripts/MenuRoom.cs
  13. 28
      Assets/FungusExample/Scripts/ParallaxRoom.cs
  14. 8
      Assets/FungusExample/Scripts/ParallaxRoom.cs.meta

BIN
Assets/Fungus/Prefabs/ParallaxSprite.prefab

Binary file not shown.

4
Assets/Fungus/Prefabs/ParallaxSprite.prefab.meta

@ -0,0 +1,4 @@
fileFormatVersion: 2
guid: fbafb8431307c42c9b1bfb9b27f6bd98
NativeFormatImporter:
userData:

8
Assets/Fungus/Scripts/CameraController.cs

@ -225,6 +225,14 @@ namespace Fungus
} }
} }
/**
* Returns the current position of the main camera.
*/
public Vector3 GetCameraPosition()
{
return mainCamera.transform.position;
}
void SetCameraZ() void SetCameraZ()
{ {
mainCamera.transform.position = new Vector3(mainCamera.transform.position.x, mainCamera.transform.position.y, game.cameraZ); mainCamera.transform.position = new Vector3(mainCamera.transform.position.x, mainCamera.transform.position.y, game.cameraZ);

23
Assets/Fungus/Scripts/Game.cs

@ -197,7 +197,7 @@ namespace Fungus
/** /**
* Gets a globally accessible game state value. * Gets a globally accessible game state value.
* @param key The key of the value. * @param key The key of the value.
* @return value The integer value for the specified key, or 0 if the key does not exist. * @return The integer value for the specified key, or 0 if the key does not exist.
*/ */
public int GetGameValue(string key) public int GetGameValue(string key)
{ {
@ -207,5 +207,26 @@ namespace Fungus
} }
return 0; return 0;
} }
/**
* Returns a parallax offset vector based on the camera position relative to the active Room.
* Higher values for the parallaxFactor yield a larger offset (appears closer to camera).
* Suggested range for good parallax effect is [0.1..0.5].
* @param parallaxFactor Scale factor to apply to camera offset vector.
* @return A parallax offset vector based on camera positon relative to current room and the parallaxFactor.
*/
public Vector3 GetParallaxOffset(float parallaxFactor)
{
if (activeRoom == null)
{
return Vector3.zero;
}
Vector3 a = activeRoom.transform.position;
Vector3 b = cameraController.GetCameraPosition();
Vector3 offset = (a - b) * parallaxFactor;
return offset;
}
} }
} }

60
Assets/Fungus/Scripts/Parallax.cs

@ -0,0 +1,60 @@
using UnityEngine;
using System.Collections;
namespace Fungus
{
/**
* Attach this component to a sprite object to apply a simple parallax scrolling effect.
* The parallax offset is calculated based on the distance from the camera to the position of the parent Room.
*/
public class Parallax : MonoBehaviour
{
/**
* Scale factor for calculating the parallax offset.
*/
public float parallaxFactor;
Vector3 startPosition;
Room parentRoom;
void Start ()
{
// Store the starting position of the sprite object
startPosition = transform.position;
// Store a reference to the parent Room object
Transform ancestor = transform.parent;
while (ancestor != null)
{
Room room = ancestor.GetComponent<Room>();
if (room != null)
{
parentRoom = room;
break;
}
ancestor = ancestor.transform.parent;
}
}
void Update ()
{
if (parentRoom == null)
{
// Don't apply offset if the sprite is not a child of a Room
return;
}
if (Game.GetInstance().activeRoom != parentRoom)
{
// Early out if this sprite is not in the currently active Room
return;
}
Vector3 offset = Game.GetInstance().GetParallaxOffset(parallaxFactor);
// Set new position for sprite
transform.position = startPosition + offset;
}
}
}

8
Assets/Fungus/Scripts/Parallax.cs.meta

@ -0,0 +1,8 @@
fileFormatVersion: 2
guid: 3afab7986c14e4b7cbd982245a28b9a5
MonoImporter:
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:

BIN
Assets/Fungus/Sprites/ButtonBlank.psd

Binary file not shown.

45
Assets/Fungus/Sprites/ButtonBlank.psd.meta

@ -0,0 +1,45 @@
fileFormatVersion: 2
guid: 0a068860829d14b48a48e6245e0e591c
TextureImporter:
serializedVersion: 2
mipmaps:
mipMapMode: 0
enableMipMap: 0
linearTexture: 0
correctGamma: 0
fadeOut: 0
borderMipMap: 0
mipMapFadeDistanceStart: 1
mipMapFadeDistanceEnd: 3
bumpmap:
convertToNormalMap: 0
externalNormalMap: 0
heightScale: .25
normalMapFilter: 0
isReadable: 0
grayScaleToAlpha: 0
generateCubemap: 0
seamlessCubemap: 0
textureFormat: -3
maxTextureSize: 1024
textureSettings:
filterMode: -1
aniso: 1
mipBias: -1
wrapMode: 1
nPOTScale: 0
lightmap: 0
compressionQuality: 50
spriteMode: 1
spriteExtrude: 1
spriteMeshType: 1
alignment: 0
spritePivot: {x: .5, y: .5}
spritePixelsToUnits: 100
alphaIsTransparency: 1
textureType: 8
buildTargetSettings: []
spriteSheet:
sprites: []
spritePackingTag:
userData:

BIN
Assets/Fungus/Sprites/Mushroom.png

Binary file not shown.

After

Width:  |  Height:  |  Size: 27 KiB

45
Assets/Fungus/Sprites/Mushroom.png.meta

@ -0,0 +1,45 @@
fileFormatVersion: 2
guid: ea8f56c43254d41728f5ac4e8299b6c9
TextureImporter:
serializedVersion: 2
mipmaps:
mipMapMode: 0
enableMipMap: 0
linearTexture: 0
correctGamma: 0
fadeOut: 0
borderMipMap: 0
mipMapFadeDistanceStart: 1
mipMapFadeDistanceEnd: 3
bumpmap:
convertToNormalMap: 0
externalNormalMap: 0
heightScale: .25
normalMapFilter: 0
isReadable: 0
grayScaleToAlpha: 0
generateCubemap: 0
seamlessCubemap: 0
textureFormat: -3
maxTextureSize: 1024
textureSettings:
filterMode: -1
aniso: 1
mipBias: -1
wrapMode: 1
nPOTScale: 0
lightmap: 0
compressionQuality: 50
spriteMode: 1
spriteExtrude: 1
spriteMeshType: 1
alignment: 0
spritePivot: {x: .5, y: .5}
spritePixelsToUnits: 100
alphaIsTransparency: 1
textureType: 8
buildTargetSettings: []
spriteSheet:
sprites: []
spritePackingTag:
userData:

BIN
Assets/FungusExample/Scenes/Example.unity

Binary file not shown.

7
Assets/FungusExample/Scripts/MenuRoom.cs

@ -7,6 +7,7 @@ public class MenuRoom : Room
public Room pageRoom; public Room pageRoom;
public Room viewRoom; public Room viewRoom;
public Room spriteRoom; public Room spriteRoom;
public Room parallaxRoom;
public Room buttonRoom; public Room buttonRoom;
public Room audioRoom; public Room audioRoom;
@ -15,6 +16,7 @@ public class MenuRoom : Room
AddOption("Writing a story with Pages", MoveToWritingRoom); AddOption("Writing a story with Pages", MoveToWritingRoom);
AddOption("Controlling the camera with Views", MoveToViewRoom); AddOption("Controlling the camera with Views", MoveToViewRoom);
AddOption("Sprites and Animations", MoveToSpriteRoom); AddOption("Sprites and Animations", MoveToSpriteRoom);
AddOption("Parallax scrolling effects", MoveToParallaxRoom);
AddOption("Using Buttons", MoveToButtonsRoom); AddOption("Using Buttons", MoveToButtonsRoom);
AddOption("Playing music and sound effects", MoveToAudioRoom); AddOption("Playing music and sound effects", MoveToAudioRoom);
Choose("Choose an example"); Choose("Choose an example");
@ -35,6 +37,11 @@ public class MenuRoom : Room
MoveToRoom(spriteRoom); MoveToRoom(spriteRoom);
} }
void MoveToParallaxRoom()
{
MoveToRoom(parallaxRoom);
}
void MoveToButtonsRoom() void MoveToButtonsRoom()
{ {
MoveToRoom(buttonRoom); MoveToRoom(buttonRoom);

28
Assets/FungusExample/Scripts/ParallaxRoom.cs

@ -0,0 +1,28 @@
using UnityEngine;
using System.Collections;
using Fungus;
// The parallax effect is achieved by attaching a Parallax script to each sprite that requires a
// parallax offset. The offset is then applied automatically whenever the camera moves around the active Room.
// There is a handy parallax sprite prefab in Fungus/Prefabs/ParallaxSprite.prefab
public class ParallaxRoom : Room
{
public View mainView;
public View zoomView;
public Room menuRoom;
void OnEnter()
{
SetView(mainView);
Say("Let's zoom in!");
PanToView(zoomView, 2);
Say("Oooh! Nice parallax!");
PanToView(mainView, 2);
Say("Mmmm... purdy!");
MoveToRoom(menuRoom);
}
}

8
Assets/FungusExample/Scripts/ParallaxRoom.cs.meta

@ -0,0 +1,8 @@
fileFormatVersion: 2
guid: 65166947e16ca4402a77071aca234c0a
MonoImporter:
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
Loading…
Cancel
Save