Skip to content

Commit 979f267

Browse files
committed
Add remote trigger and character spawner
relates #136
1 parent 8bc615c commit 979f267

File tree

7 files changed

+169
-6
lines changed

7 files changed

+169
-6
lines changed

src/pyherc/data/level.hy

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -152,9 +152,9 @@
152152

153153
(defn safe-passage [level location]
154154
"check if given location is free to move without danger"
155-
(all [(not (blocks-movement level location))
156-
(not (get-traps level location))
157-
(not (get-character level location))]))
155+
(and (not (blocks-movement level location))
156+
(not (get-traps level location))
157+
(not (get-character level location))))
158158

159159
(defn ornamentation [level location &optional [tile-id "no-tile"]]
160160
(assert (!= tile-id []))

src/pyherc/data/traps/__init__.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,3 +28,5 @@
2828

2929
from .caltrops import Caltrops
3030
from .pit import PitTrap
31+
from .spawner import CharacterSpawner
32+
from .triggers import RemoteTrigger

src/pyherc/data/traps/spawner.hy

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
;; -*- coding: utf-8 -*-
2+
;;
3+
;; Copyright (c) 2010-2015 Tuukka Turto
4+
;;
5+
;; Permission is hereby granted, free of charge, to any person obtaining a copy
6+
;; of this software and associated documentation files (the "Software"), to deal
7+
;; in the Software without restriction, including without limitation the rights
8+
;; to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
;; copies of the Software, and to permit persons to whom the Software is
10+
;; furnished to do so, subject to the following conditions:
11+
;;
12+
;; The above copyright notice and this permission notice shall be included in
13+
;; all copies or substantial portions of the Software.
14+
;;
15+
;; THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
;; IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
;; FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
;; AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
;; LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
;; OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21+
;; THE SOFTWARE.
22+
23+
(require pyherc.macros)
24+
(require hy.contrib.anaphoric)
25+
26+
(import [random]
27+
[pyherc.data.traps.trap [Trap]]
28+
[pyherc.data [blocks-movement add-character]]
29+
[pyherc.data.geometry [area-around]])
30+
31+
(defclass CharacterSpawner [Trap]
32+
[[--init-- (fn [self character-selector &optional [icon nil]]
33+
(super-init icon)
34+
(set-attributes character-selector)
35+
nil)]
36+
[on-trigger (fn [self]
37+
(let [[creatures (self.character-selector)]
38+
[area (area-around self.location)]]
39+
(ap-each creatures (place-creature it self.level area))))]])
40+
41+
(defn place-creature [creature level area]
42+
(let [[free-spots (list (ap-filter (not (blocks-movement level it))
43+
area))]]
44+
(when free-spots
45+
(add-character level
46+
(.choice random free-spots)
47+
creature))))

src/pyherc/data/traps/trap.hy

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,15 @@
2626
(setv self.location nil)
2727
(setv self.icon icon)
2828
nil)]
29-
[on-enter (fn [self character])]
30-
[on-item-enter (fn [self item])]
31-
[on-place (fn [self level location])]])
29+
[on-enter (fn [self character]
30+
"called when a character enters square with trap"
31+
nil)]
32+
[on-item-enter (fn [self item]
33+
"called when item enters square with trap"
34+
nil)]
35+
[on-place (fn [self level location]
36+
"called when trap is placed"
37+
nil)]
38+
[on-trigger (fn [self]
39+
"called when trap is remotely triggered"
40+
nil)]])

src/pyherc/data/traps/triggers.hy

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
;; -*- coding: utf-8 -*-
2+
;;
3+
;; Copyright (c) 2010-2015 Tuukka Turto
4+
;;
5+
;; Permission is hereby granted, free of charge, to any person obtaining a copy
6+
;; of this software and associated documentation files (the "Software"), to deal
7+
;; in the Software without restriction, including without limitation the rights
8+
;; to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
;; copies of the Software, and to permit persons to whom the Software is
10+
;; furnished to do so, subject to the following conditions:
11+
;;
12+
;; The above copyright notice and this permission notice shall be included in
13+
;; all copies or substantial portions of the Software.
14+
;;
15+
;; THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
;; IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
;; FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
;; AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
;; LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
;; OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21+
;; THE SOFTWARE.
22+
23+
(require pyherc.macros)
24+
25+
(import [pyherc.data.traps.trap [Trap]])
26+
27+
(defclass RemoteTrigger [Trap]
28+
[[--init-- (fn [self trap &optional [icon nil]]
29+
(super-init icon)
30+
(set-attributes trap)
31+
nil)]
32+
[on-enter (fn [self character]
33+
(when (and self.trap
34+
self.trap.level)
35+
(.on-trigger self.trap)))]
36+
[on-item-enter (fn [self item]
37+
(when (and self.trap
38+
self.trap.level)
39+
(.on-trigger self.trap)))]])

src/pyherc/test/__init__.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@
4747
from pyherc.test.unit.test_shoes import *
4848
from pyherc.test.unit.test_skills import *
4949
from pyherc.test.unit.test_solver import *
50+
from pyherc.test.unit.test_spawner import *
5051
from pyherc.test.unit.test_surround_decorator import *
5152
from pyherc.test.unit.test_time import *
5253
from pyherc.test.unit.test_trapgeneration import *

src/pyherc/test/unit/test_spawner.hy

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
;; -*- coding: utf-8 -*-
2+
;;
3+
;; Copyright (c) 2010-2015 Tuukka Turto
4+
;;
5+
;; Permission is hereby granted, free of charge, to any person obtaining a copy
6+
;; of this software and associated documentation files (the "Software"), to deal
7+
;; in the Software without restriction, including without limitation the rights
8+
;; to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
;; copies of the Software, and to permit persons to whom the Software is
10+
;; furnished to do so, subject to the following conditions:
11+
;;
12+
;; The above copyright notice and this permission notice shall be included in
13+
;; all copies or substantial portions of the Software.
14+
;;
15+
;; THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
;; IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
;; FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
;; AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
;; LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
;; OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21+
;; THE SOFTWARE.
22+
23+
(require pyherc.macros)
24+
25+
(import [hamcrest [assert-that has-item]]
26+
[pyherc.data.traps [RemoteTrigger CharacterSpawner]]
27+
[pyherc.data.constants [Direction]]
28+
[pyherc.data [add-character add-trap get-characters]]
29+
[pyherc.ports [set-action-factory move]]
30+
[pyherc.test.builders [LevelBuilder CharacterBuilder
31+
ActionFactoryBuilder]])
32+
33+
(defn setup []
34+
"setup test case"
35+
(let [[level (-> (LevelBuilder)
36+
(.build))]
37+
[character₀ (-> (CharacterBuilder)
38+
(.build))]
39+
[character₁ (-> (CharacterBuilder)
40+
(.build))]
41+
[spawner (CharacterSpawner (fn [] [character₁]))]
42+
[trigger (RemoteTrigger spawner)]]
43+
(set-action-factory (-> (ActionFactoryBuilder)
44+
(.with-move-factory)
45+
(.build)))
46+
{:level level
47+
:spawner spawner
48+
:trigger trigger
49+
:character₀ character₀
50+
:character₁ character₁}))
51+
52+
(defn test-spawning-character []
53+
"character spawner can create new character on level"
54+
(let [[context (setup)]
55+
[level (:level context)]
56+
[character₀ (:character₀ context)]
57+
[character₁ (:character₁ context)]
58+
[spawner (:spawner context)]
59+
[trigger (:trigger context)]]
60+
(add-character level #t(1 1) character₀)
61+
(add-trap level #t(2 1) trigger)
62+
(add-trap level #t(5 5) spawner)
63+
(move character₀ Direction.east)
64+
(assert-that (list (get-characters level))
65+
(has-item character₁))))

0 commit comments

Comments
 (0)