Permalink
Browse files

Fix my dumb zoom shit

  • Loading branch information...
Koraken committed Nov 13, 2017
1 parent 7f46ade commit 7baecfb2aa5992875a6f5328e207e9e1832f36c2
Showing with 43 additions and 86 deletions.
  1. +5 −7 actions.py
  2. +5 −5 expressions.py
  3. +7 −3 ghost.py
  4. +15 −7 main.py
  5. +3 −1 pixelsprite.py
  6. +8 −4 scenes.py
  7. +0 −59 scrollablezoom.py
View
@@ -1,5 +1,6 @@
from cocos.actions import IntervalAction
from cocos.euclid import *
from cocos.director import director
class ScaleXTo(IntervalAction):
"""Scales a `CocosNode` object to a zoom factor by modifying it's scale attribute.
@@ -130,7 +131,7 @@ def __reversed__(self):
return MoveBy(-self.delta, self.duration)
class ChangeFocus(IntervalAction):
def init(self, focusX, focusY, focusZoom, duration=5):
def init(self, focusX, focusY, duration=5):
"""Init method.
:Parameters:
@@ -141,20 +142,17 @@ def init(self, focusX, focusY, focusZoom, duration=5):
"""
self.end_posx = focusX
self.end_posy = focusY
self.end_zoom = focusZoom
self.duration = duration
print("init")
def start(self):
print("start")
self.start_posx = self.target.getCenterX()
self.start_posy = self.target.getCenterY()
self.start_zoom = self.target.zoom
self.start_posx = director.scroller.fx
self.start_posy = director.scroller.fy
self.posxdelta = self.end_posx - self.start_posx
self.posydelta = self.end_posy - self.start_posy
self.zoomdelta = self.end_zoom - self.start_zoom
def update(self, t):
print("update")
self.target.set_focus(self.start_posx + self.posxdelta*t, self.start_posy + self.posydelta*t, zoomLevel=self.start_zoom + self.zoomdelta*t)
self.target.set_focus(self.start_posx + self.posxdelta*t, self.start_posy + self.posydelta*t)
View
@@ -9,18 +9,18 @@ def __init__(self, ghost):
self.rightEye = ghost.rightEye
self.leftEye.uLid.offset = self.leftEye.lLid.offset = self.rightEye.uLid.offset = self.rightEye.lLid.offset = (0,0)
self.activeExpression = None
self.Do(Default)
self.do(Default)
def Do(self, exp):
def do(self, exp):
self.activeExpression = exp, time.time()
exp.Do(self.leftEye, self.rightEye, self.lastExpression)
exp.do(self.leftEye, self.rightEye, self.lastExpression)
if exp.hold:
self.lastExpression = exp
def Update(self, dt):
if self.activeExpression and not self.activeExpression[0].hold:
if time.time() >= (self.activeExpression[1] + self.activeExpression[0].duration):
self.Do(self.lastExpression)
self.do(self.lastExpression)
class Default():
@@ -32,7 +32,7 @@ class Default():
duration = 0.2
@classmethod
def Do(cls, leftEye, rightEye, last):
def do(cls, leftEye, rightEye, last):
leftEye.uLid.do(ScaleYTo(cls.leftULidScale, cls.duration))
leftEye.lLid.do(ScaleYTo(cls.leftLLidScale, cls.duration))
rightEye.uLid.do(ScaleYTo(cls.rightULidScale, cls.duration))
View
@@ -10,7 +10,7 @@
from pixelsprite import PixelSprite
from spritegroup import SpriteGroup
from actions import MoveToWhole
from actions import MoveToWhole, ChangeFocus
from expressions import *
from util import Clamp, VecDistance
@@ -161,13 +161,17 @@ def KeyReleased(self, key, heldKeys):
self.moving = False
if key == 'SPACE':
director.scroller.do(ScaleTo(1, duration=1))
self.focusGhost = None
def Update(self, dt):
# Focusing on ghost stuffs
if self.focusGhost:
self.focusPoint = self.focusGhost.position
director.scroller.do(ScaleTo(2, duration=1))
director.scroller.do(ChangeFocus(self.focusGhost.position[0], self.focusGhost.position[1], duration=1))
if VecDistance(self.position, self.focusGhost.position) > self.focusDistance:
director.scroller.do(ScaleTo(1, duration=1))
self.focusGhost = None
else:
self.focusPoint = None
@@ -193,9 +197,9 @@ def FindCloseGhost(self):
def on_mouse_press(self, x, y, button, modifiers):
if button == 1:
self.Expressions.Do(Blink)
self.Expressions.do(Blink)
elif button == 4:
self.ExpressionNum += 1
if self.ExpressionNum >= len(self.ExpressionList):
self.ExpressionNum = 0
self.Expressions.Do(self.ExpressionList[self.ExpressionNum])
self.Expressions.do(self.ExpressionList[self.ExpressionNum])
View
22 main.py
@@ -1,22 +1,30 @@
import cocos
from cocos import scene
from cocos.scene import Scene
from cocos.director import director
from cocos.layer import ColorLayer, ScrollingManager
from scrollablezoom import ScrollingZoomManager
from scenes import Game
windowWidth = 800
windowHeight = 600
director.init(width=windowWidth, height=windowHeight, caption="Ghosts")
# Putting the scroller in the director so we can access it everywhere we need to without reimporting main
director.scroller = ScrollingZoomManager()
director.scroller = ScrollingManager()
gameLayer = Game()
director.scroller.add(gameLayer)
director.scroller.set_focus(windowWidth/2, windowHeight/2, zoomLevel=1)
director.scroller.add(gameLayer, z=1)
scene = scene.Scene(gameLayer)
# Then I make a ColorLayer, just to spice up the background a bit (which for now is just transparent)
bg_color = ColorLayer(52, 152, 219, 255)
director.autoscale = True
# Then I make a scene
scene = Scene()
# And I add the scroller to it and put it on top of the stack of layers
scene.add(director.scroller, z=1)
# And I add the background color (I don't need to define a z because by default it's 0)
scene.add(bg_color, z=0)
# And then we run it!
director.run(scene)
View
@@ -22,4 +22,6 @@ def __init__(self, img, position=(0, 0), rotation=0, scale=1, scale_x=1, scale_y
# This adjusts for that
def point_to_world(self, pos):
basePos = super().point_to_world(pos)
return euclid.Point2(basePos[0] + director.scroller.fx, basePos[1] + director.scroller.fy)
size = director.get_window_size()
return euclid.Point2(basePos[0] + director.scroller.fx - (size[0] / (2 * director.scroller.scale)),
basePos[1] + director.scroller.fy - (size[1] / (2 * director.scroller.scale)))
View
@@ -1,12 +1,13 @@
import cocos
from cocos.layer import ScrollableLayer
from cocos.director import director
from pixelsprite import PixelSprite
from ghost import Ghost, PlayerGhost
from scrollablezoom import ScrollableZoomLayer
from pyglet.window.key import symbol_string
class Game(ScrollableZoomLayer):
class Game(ScrollableLayer):
is_event_handler = True
def Update(self, dt, *args):
@@ -15,8 +16,6 @@ def Update(self, dt, *args):
def __init__(self):
super().__init__()
self.add(PixelSprite('assets/img/blank.png', scale_x=800, scale_y=600, color=(80,80,150)))
self.ghosts = []
self.heldKeys = {}
@@ -34,6 +33,11 @@ def __init__(self):
self.schedule(self.Update, self.ghosts)
def on_enter(self):
super().on_enter()
size = director.get_window_size()
director.scroller.set_focus(size[0] / 2, size[1] / 2)
def addGhost(self, ghost):
self.ghosts.append(ghost)
self.add(ghost)
View

This file was deleted.

Oops, something went wrong.

0 comments on commit 7baecfb

Please sign in to comment.