diff --git a/Assets/Fungus/Prefabs/ParallaxSprite.prefab b/Assets/Fungus/Prefabs/ParallaxSprite.prefab new file mode 100644 index 00000000..977746e4 Binary files /dev/null and b/Assets/Fungus/Prefabs/ParallaxSprite.prefab differ diff --git a/Assets/Fungus/Prefabs/ParallaxSprite.prefab.meta b/Assets/Fungus/Prefabs/ParallaxSprite.prefab.meta new file mode 100644 index 00000000..ad410e4e --- /dev/null +++ b/Assets/Fungus/Prefabs/ParallaxSprite.prefab.meta @@ -0,0 +1,4 @@ +fileFormatVersion: 2 +guid: fbafb8431307c42c9b1bfb9b27f6bd98 +NativeFormatImporter: + userData: diff --git a/Assets/Fungus/Scripts/CameraController.cs b/Assets/Fungus/Scripts/CameraController.cs index fc3f97b0..5c66689a 100644 --- a/Assets/Fungus/Scripts/CameraController.cs +++ b/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() { mainCamera.transform.position = new Vector3(mainCamera.transform.position.x, mainCamera.transform.position.y, game.cameraZ); diff --git a/Assets/Fungus/Scripts/Game.cs b/Assets/Fungus/Scripts/Game.cs index 5b88e1f4..4d642180 100644 --- a/Assets/Fungus/Scripts/Game.cs +++ b/Assets/Fungus/Scripts/Game.cs @@ -197,7 +197,7 @@ namespace Fungus /** * Gets a globally accessible game state 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) { @@ -207,5 +207,26 @@ namespace Fungus } 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; + } } } \ No newline at end of file diff --git a/Assets/Fungus/Scripts/Parallax.cs b/Assets/Fungus/Scripts/Parallax.cs new file mode 100644 index 00000000..c893281e --- /dev/null +++ b/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(); + 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; + } + } +} diff --git a/Assets/Fungus/Scripts/Parallax.cs.meta b/Assets/Fungus/Scripts/Parallax.cs.meta new file mode 100644 index 00000000..c54e444a --- /dev/null +++ b/Assets/Fungus/Scripts/Parallax.cs.meta @@ -0,0 +1,8 @@ +fileFormatVersion: 2 +guid: 3afab7986c14e4b7cbd982245a28b9a5 +MonoImporter: + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: diff --git a/Assets/Fungus/Sprites/ButtonBlank.psd b/Assets/Fungus/Sprites/ButtonBlank.psd new file mode 100644 index 00000000..13bf041a Binary files /dev/null and b/Assets/Fungus/Sprites/ButtonBlank.psd differ diff --git a/Assets/Fungus/Sprites/ButtonBlank.psd.meta b/Assets/Fungus/Sprites/ButtonBlank.psd.meta new file mode 100644 index 00000000..9c9cb1c6 --- /dev/null +++ b/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: diff --git a/Assets/Fungus/Sprites/Mushroom.png b/Assets/Fungus/Sprites/Mushroom.png new file mode 100644 index 00000000..7b4d51bf Binary files /dev/null and b/Assets/Fungus/Sprites/Mushroom.png differ diff --git a/Assets/Fungus/Sprites/Mushroom.png.meta b/Assets/Fungus/Sprites/Mushroom.png.meta new file mode 100644 index 00000000..ea923a95 --- /dev/null +++ b/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: diff --git a/Assets/FungusExample/Scenes/Example.unity b/Assets/FungusExample/Scenes/Example.unity index 7f5bf656..f46a08e3 100644 Binary files a/Assets/FungusExample/Scenes/Example.unity and b/Assets/FungusExample/Scenes/Example.unity differ diff --git a/Assets/FungusExample/Scripts/MenuRoom.cs b/Assets/FungusExample/Scripts/MenuRoom.cs index 04db5223..fd87515d 100644 --- a/Assets/FungusExample/Scripts/MenuRoom.cs +++ b/Assets/FungusExample/Scripts/MenuRoom.cs @@ -7,6 +7,7 @@ public class MenuRoom : Room public Room pageRoom; public Room viewRoom; public Room spriteRoom; + public Room parallaxRoom; public Room buttonRoom; public Room audioRoom; @@ -15,6 +16,7 @@ public class MenuRoom : Room AddOption("Writing a story with Pages", MoveToWritingRoom); AddOption("Controlling the camera with Views", MoveToViewRoom); AddOption("Sprites and Animations", MoveToSpriteRoom); + AddOption("Parallax scrolling effects", MoveToParallaxRoom); AddOption("Using Buttons", MoveToButtonsRoom); AddOption("Playing music and sound effects", MoveToAudioRoom); Choose("Choose an example"); @@ -35,6 +37,11 @@ public class MenuRoom : Room MoveToRoom(spriteRoom); } + void MoveToParallaxRoom() + { + MoveToRoom(parallaxRoom); + } + void MoveToButtonsRoom() { MoveToRoom(buttonRoom); diff --git a/Assets/FungusExample/Scripts/ParallaxRoom.cs b/Assets/FungusExample/Scripts/ParallaxRoom.cs new file mode 100644 index 00000000..b166c972 --- /dev/null +++ b/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); + } +} diff --git a/Assets/FungusExample/Scripts/ParallaxRoom.cs.meta b/Assets/FungusExample/Scripts/ParallaxRoom.cs.meta new file mode 100644 index 00000000..86869b9d --- /dev/null +++ b/Assets/FungusExample/Scripts/ParallaxRoom.cs.meta @@ -0,0 +1,8 @@ +fileFormatVersion: 2 +guid: 65166947e16ca4402a77071aca234c0a +MonoImporter: + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: