Skip to content

Commit

Permalink
Pass and use dt, update EAF to 0.2
Browse files Browse the repository at this point in the history
Signed-off-by: Pavel Kulyov <[email protected]>
  • Loading branch information
pkulev committed May 4, 2020
1 parent b5372fc commit 5bb3047
Show file tree
Hide file tree
Showing 11 changed files with 69 additions and 72 deletions.
4 changes: 2 additions & 2 deletions README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,8 @@ General
-------

* >= python-3.7
* >= eaf-0.1
* >= eaf-0.2
* >= xo1-0.1
* >= toml-0.10
* ncurses >=5.9

Expand All @@ -35,7 +36,6 @@ Development
.. code-block:: console
$ poetry install
$ poetry run xoigame
Testing
Expand Down
11 changes: 6 additions & 5 deletions pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[tool.poetry]
name = "xoinvader"
version = "0.1.3"
version = "0.2.0"
description = "(not so) small python curses space game"
authors = ["Pavel Kulyov <[email protected]>"]
license = "MIT"
Expand All @@ -20,10 +20,10 @@ classifiers = [
"Intended Audience :: Education",
"Intended Audience :: End Users/Desktop",
"License :: OSI Approved :: MIT License",
"Natural Language :: English",
"Operating System :: POSIX :: Linux",
"Programming Language :: Python :: 2.7",
"Programming Language :: Python :: 3",
"Programming Language :: Python :: 3.7",
"Programming Language :: Python :: 3.8",
"Programming Language :: Python :: Implementation :: CPython",
"Topic :: Games/Entertainment :: Arcade",
"Topic :: Software Development :: Libraries :: pygame",
Expand All @@ -34,8 +34,9 @@ xoigame = "xoinvader.game:main"

[tool.poetry.dependencies]
python = "^3.7"
eaf = "*"
toml = ">=0.10"
eaf = "^0.2"
xo1 = "^0.1"
toml = "^0.10"

[tool.poetry.dev-dependencies]
codecov = "*"
Expand Down
12 changes: 6 additions & 6 deletions xoinvader/animation.py
Original file line number Diff line number Diff line change
Expand Up @@ -77,14 +77,14 @@ def add(self, name, *args, **kwargs):
if not self._animation:
self._animation = animation

def update(self):
def update(self, dt):
"""Update manager's state."""

if not self._animation:
return

try:
self._animation.update()
self._animation.update(dt)
except StopIteration:
return # TODO: think about method to change animation

Expand Down Expand Up @@ -152,7 +152,7 @@ def _apply_value(self, value):

setattr(self._obj, self._attr, value)

def _update_interpolated(self):
def _update_interpolated(self, dt):
"""Advance animation and interpolate value.
NOTE: animation frame switching depends on interp mode
Expand All @@ -161,7 +161,7 @@ def _update_interpolated(self):
"""

self._check_animation_state()
self._timer.update()
self._timer.update(dt)

current_time = self._timer.get_elapsed()
keyframe = self._keyframes[self._current]
Expand All @@ -183,7 +183,7 @@ def _update_interpolated(self):
value = interpolate(keyframe, next_keyframe, current_time)
self._apply_value(value)

def _update_discrete(self):
def _update_discrete(self, dt):
"""Advance animation without interpolating value.
NOTE: animation frame switching depends on interp mode
Expand All @@ -194,7 +194,7 @@ def _update_discrete(self):
"""

self._check_animation_state()
self._timer.update()
self._timer.update(dt)

keyframe = self._keyframes[self._current]

Expand Down
25 changes: 10 additions & 15 deletions xoinvader/background.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
"""Level background."""

from xoinvader import app
from xoinvader.common import Settings
from xoinvader.render import Renderable
from xoinvader.utils import Point, Surface
Expand Down Expand Up @@ -142,7 +143,7 @@ def __init__(self, filename=None, speed=0, loop=False, loop_all=False):
self._current_chunk = None
self._current_chunk_num = 0 # position of chunk in chunk list
self._chunk_line = 0 # position in current chunk
self._ticks_since_last_update = 0
self._elapsed_ms = 0

if filename:
self.load_file(filename)
Expand Down Expand Up @@ -230,14 +231,8 @@ def clear(self):

self._background = [" " * self._w] * self._h

def load_file(self, filename):
"""Load background from file.
Calls `load_chunks` function int trim mode, and stores return value in
the `chunks` field.
:param str filename:
"""
def load_file(self, filename: str):
"""Load background from file."""

self._chunks = load_chunks(filename, self._w)

Expand All @@ -262,7 +257,7 @@ def start(self, filled=False):
:param bool filled: if true, perform initial background fill
"""

self._ticks_since_last_update = 0
self._elapsed_ms = 0
self._current_chunk_num = 0
self._current_chunk = self._chunks[0]
self._chunk_line = 0
Expand Down Expand Up @@ -349,22 +344,22 @@ def update_surface(self):

self._background_surface = Surface(self._background)

def update(self):
def update(self, dt):
"""Update background.
Checks if time to update has come, and calls `_advance_chunk` method
with appropriate parameter. Calls `update_surface` at the end.
"""

if self._speed == 0:
return

self._ticks_since_last_update += abs(self._speed)
if self._ticks_since_last_update < 60: # TODO FIXME: hardcode wololo
# FIXME: now this doesn't work properly
self._elapsed_ms += dt * abs(self._speed)
if self._elapsed_ms / 1000 > (1.0 / app.current().fps * 1000 * abs(self._speed)):
return

self._ticks_since_last_update = 0
self._elapsed_ms = 0

advance = 1 if self._speed > 0 else -1
new_line = self._advance_chunk(advance)
Expand Down
4 changes: 2 additions & 2 deletions xoinvader/charge.py
Original file line number Diff line number Diff line change
Expand Up @@ -74,10 +74,10 @@ def remove_obsolete(self, pos):
app.current().state.remove(self)
self._destroy = True

def update(self):
def update(self, dt):
"""Update coords."""

self._pos += Point(self._dx, self._dy)
self._pos += Point(self._dx, self._dy) * dt / 1000

def destroy(self):
"""Self-destroying routine."""
Expand Down
12 changes: 6 additions & 6 deletions xoinvader/config/xoinvader.toml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ level1bg = "/res/level1.bg"
scoreboard = "/data/scoreboard"

[ship.PlayerShip]
dx = 1
dx = 40
hull = 100
shield = 100
max_hull = 100
Expand Down Expand Up @@ -41,25 +41,25 @@ cooldown = 2
[weapon.EBlaster]
ammo = "infinite"
max_ammo = "infinite"
cooldown = 0.7
cooldown = 1.2


[charge.BasicPlasmaCannon]
damage = 3
radius = 0
dy = -1
dy = -20

[charge.EBasicPlasmaCannon]
damage = 60
radius = 1
dy = 1
dy = 20

[charge.BasicLaserCharge]
damage = 10
radius = 0
dy = -2
dy = -25

[charge.BasicUnguidedMissile]
damage = 50
radius = 3
dy = -1
dy = -15
14 changes: 7 additions & 7 deletions xoinvader/gui.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ def _make_image(self) -> Surface:
[[_style for _ in range(len(self._text))]],
)

def update(self, text: Optional[str] = None, style: Optional[int] = None):
def update(self, dt: int, text: Optional[str] = None, style: Optional[int] = None):
"""Obtain (or not) new data and refresh image.
:param text: new text
Expand Down Expand Up @@ -82,7 +82,7 @@ def __init__(

super(TextCallbackWidget, self).__init__(pos, callback(), style)

def update(self):
def update(self, dt):
self._text = self._callback()
self._image = self._make_image()

Expand Down Expand Up @@ -225,7 +225,7 @@ def current(self):
def get_render_data(self):
return [], []

def update(self):
def update(self, dt):
pass

def get_renderable_objects(self):
Expand Down Expand Up @@ -268,9 +268,9 @@ def _finalize_cb(self):
if self._callback:
self._callback(self)

def update(self, text=None, style=None):
def update(self, dt: int, text: Optional[str] = None, style: Optional[int] = None):
self._update_text(text, style)
self._timer.update()
self._timer.update(dt)


class WeaponWidget(Renderable):
Expand Down Expand Up @@ -302,7 +302,7 @@ def _make_image(self):
[[Style().gui["yellow"] for _ in range(len(self._data))]],
)

def update(self):
def update(self, dt):
"""Obtain new data and refresh image."""

self._data = self._get_data()
Expand Down Expand Up @@ -418,7 +418,7 @@ def _update_image(self):
[[ch[0] for ch in image]], [[st[1] for st in image]]
)

def update(self, val=None):
def update(self, dt: int, val=None):
"""Update bar if there's need for it."""

if self._callback:
Expand Down
6 changes: 3 additions & 3 deletions xoinvader/ingame.py
Original file line number Diff line number Diff line change
Expand Up @@ -226,10 +226,10 @@ def _create_gui(self):
def events(self):
self._events.handle()

def update(self):
def update(self, dt):
self.collision.update()
self.level.update()
if not self.level.running:
self.level.start()

super().update()
LOG.info(self._objects)
super().update(dt)
26 changes: 14 additions & 12 deletions xoinvader/ship.py
Original file line number Diff line number Diff line change
Expand Up @@ -132,22 +132,22 @@ def add_weapon(self, weapon):
self._weapons.append(weapon)
self._weapon = weapon

def update_position(self):
def update_position(self, dt):
"""Update ship position.
Allows to go behind field borders.
"""

self._pos.x += self._direction * self._dx
self._pos.x += self._direction * self._dx * dt
self._direction = 0

def update(self):
def update(self, dt):
"""Update ship object's state."""

self.update_position()
self.update_position(dt)

for weapon in self._weapons:
weapon.update()
weapon.update(dt)

if self._fire:
try:
Expand Down Expand Up @@ -234,7 +234,7 @@ def __init__(self, pos):
def add_animation(self, *args, **kwargs):
self._animgr.add(*args, **kwargs)

def update(self):
def update(self, dt):
if self._hull <= 0:
# TODO: [scoring]
# * Parametrize scores, move them to ships.conf
Expand All @@ -243,9 +243,9 @@ def update(self):
self.destroy()
return

self._animgr.update()
self._animgr.update(dt)

super(GenericXEnemy, self).update()
super(GenericXEnemy, self).update(dt)

@collision.register("GenericXEnemy", "BasicPlasmaCannon")
@collision.register("GenericXEnemy", "BasicLaserCharge")
Expand Down Expand Up @@ -283,7 +283,7 @@ def __init__(self, pos):
self._weapon = self._weapons.current()
self._wbay = Point(x=self._image.width // 2, y=-1)

def update_position(self):
def update_position(self, dt):
"""Update player ship position.
Prohibits moving out behind the border.
Expand All @@ -299,17 +299,19 @@ def update_position(self):
self._pos.x = 1

else:
self._pos.x += self._direction * self._dx
# NOTE: Converting float to int reduces ship teleportation because
# we have no pixels.
self._pos.x += int(self._direction * self._dx * dt / 1000)

self._direction = 0

def update(self):
def update(self, dt):
if self._hull <= 0:
app.current().trigger_state(
"GameOverState", score=app.current().state.score
)

super(PlayerShip, self).update()
super(PlayerShip, self).update(dt)

def get_weapon_info(self):
"""Return information about current weapon."""
Expand Down
Loading

0 comments on commit 5bb3047

Please sign in to comment.