123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507 |
- import os
- import json
- import gi
- gi.require_version('Gtk', '3.0')
- from gi.repository import Gtk
- import cairo
- import math
- from modules import ui
- from modules import world
- def layer(game):
-
-
- surface = cairo.ImageSurface(cairo.FORMAT_ARGB32,
- game.current['w'],
- game.current['h'])
- layer = cairo.Context(surface)
- layer.set_antialias(cairo.ANTIALIAS_NONE)
-
- withFPS = (1/game.FPS*60)
-
-
- world.draw(game, layer, 0, 0,
- game.current["w"],
- game.current["h"], grid=False)
-
-
- for n, asset in enumerate(game.current["state"]["controlling"]):
- if game.current["state"]["controlling"][asset]:
- ui.color(game, layer, "orange")
- layer.rectangle(
- 14,
- 14 + (n*15),
- 12,12)
- layer.fill()
- if "following" in game.current["state"]["objects"][asset]:
- target = game.current["state"]["objects"][asset]["following"]
-
-
- ui.color(game, layer, "orange")
- layer.set_line_width(1)
- layer.move_to(
- 26,
- 14 + (n*15) + 10)
- layer.line_to(26 + (n*5) + 5,
- 14 + (n*15) + 10)
- layer.line_to(26 + (n*5) + 5,
- 14 + (target*15) + 2)
- layer.line_to(26,
- 14 + (target*15) + 2)
- layer.stroke()
- layer.move_to(28,
- 14 + (target*15))
- layer.line_to(28,
- 14 + (target*15)+3)
- layer.stroke()
-
- asset_draw = game.current["state"]["objects"][asset]["asset"]
- ui.image(game, layer,
- 15,
- 15 + (n*15) ,
- "assets/elements/"+asset_draw+".png",
- "icon")
-
- rotations = {
- "normal":[
- "Right",
- "Down-Right",
- "Down",
- "Down-Left",
- "Left",
- "Up-Left",
- "Up",
- "Up-Right"]}
-
-
- for asset, t in enumerate(game.current["state"]["objects"]):
- try:
- if "target-orientation" in game.current["state"]["objects"][asset]\
- and game.current["frame"] % max(1, int(game.FPS/10)) == 0:
- target = game.current["state"]["objects"][asset]["target-orientation"]
- current = game.current["state"]["objects"][asset]["orientation"]
- if target != current:
-
- tlist = rotations["normal"].copy()
- for i in range(2):
- for b in rotations["normal"]:
- tlist.append(b)
-
- tlist[tlist.index(current)] = "blank"
- tlist[tlist.index(current)] = "current"
- tlist[tlist.index(current)] = "blank"
- tlist[tlist.index(target)] = "option1"
- tlist[tlist.index(target)] = "option2"
- tlist[tlist.index(target)] = "option3"
- add = 1
- closest = len(tlist)
-
- for i in ["option1", "option2", "option3"]:
- t = tlist.index(i)
- c = tlist.index("current")
- if max(t,c) - min(t,c) < closest:
- closest = max(t,c) - min(t,c)
- if t < c:
- add = -1
- else:
- add = 1
- index = (rotations["normal"].index(current)+add) % len(rotations["normal"])
- put = rotations["normal"][index]
- game.current["state"]["objects"][asset]["orientation"] = put
-
- elif game.current["frame"] % max(1, int(game.FPS/10)) == 0:
- del game.current["state"]["objects"][asset]["target-orientation"]
-
- except:
- pass
- xob = game.current["state"]["objects"][asset]["xyz"][0]
- yob = game.current["state"]["objects"][asset]["xyz"][1]
-
- asset_draw = game.current["state"]["objects"][asset]["asset"]
- orientation = "Down-Left"
- try:
-
- image = game.images["assets/elements/"+asset_draw+".png"][orientation][0]["binary"]
- image_height = image.get_height()
- except:
- image_height = 61
-
- if game.current["state"]["objects"][asset]["health"] <= 0:
- game.current["state"]["objects"][asset]["health"] = 0
- game.current["state"]["objects"][asset]["orientation"] = "Dead"
- try:
- del game.current["state"]["controlling"][asset]
- except:
- pass
- continue
- else:
-
- game.current["state"]["objects"][asset]["health"] += 0.0001 * withFPS
- if game.current["state"]["objects"][asset]["health"] >= 1:
- game.current["state"]["objects"][asset]["health"] = 1
- if game.current["state"]["controlling"].get(asset, False):
- ui.color(game, layer, "orange")
- ui.text(game,layer, game.images["assets/elements/"+asset_draw+".png"].get("title", "Unknown"),
- 15,
- game.current["h"]-40)
-
- ui.fancy_health_bar(game,
- 15,
- game.current["h"]-20,
- game.current["w"]/3,
- 15,
- layer,
- game.current["state"]["objects"][asset]["health"])
- else:
- ui.color(game, layer, "red")
- layer.set_line_width(2)
- layer.move_to(xob + game.current["camera"][0]-10,
- yob + game.current["camera"][1] - image_height-5)
- layer.line_to(xob + game.current["camera"][0]+10,
- yob + game.current["camera"][1] - image_height-5)
- layer.stroke()
- ui.color(game, layer, "green")
- layer.set_line_width(2)
- layer.move_to(xob + game.current["camera"][0]-10,
- yob + game.current["camera"][1] - image_height-5)
- layer.line_to(xob + game.current["camera"][0]-10+(20*game.current["state"]["objects"][asset]["health"]),
- yob + game.current["camera"][1] - image_height-5)
- layer.stroke()
-
- if game.current["state"]["controlling"].get(asset, False):
-
- x79 = game.current["state"]["objects"][asset]["xyz"][0]
- y79 = game.current["state"]["objects"][asset]["xyz"][1]
- z79 = game.current["state"]["objects"][asset]["xyz"][2]
-
- ui.image(game, layer,
- x79 + game.current["camera"][0],
- y79 + game.current["camera"][1] + 61 - image_height,
- "assets/elements/characters/shadow.png",
- "arrow",
- offset=True, dynamic=True)
-
-
- def do():
- if "following" in game.current["state"]["objects"][asset]:
- del game.current["state"]["objects"][asset]["following"]
- else:
- follow_closest(game, asset)
- ui.button(game, layer,
- int(game.current["w"]) - 36,
- 15,
- 12,
- 12,
- icon="radio_transition",
- func=do)
-
-
-
- if "following" in game.current["state"]["objects"][asset]:
-
- target_asset = game.current["state"]["objects"][asset]["following"]
- xtar = 0
- ytar = 0
- ztar = 0
- try:
- xtar = game.current["state"]["objects"][target_asset]["xyz"][0]
- ytar = game.current["state"]["objects"][target_asset]["xyz"][1]
- ztar = game.current["state"]["objects"][target_asset]["xyz"][2]
- except:
- pass
- camx = game.current["camera"][0]
- camy = game.current["camera"][1]
- ui.color(game, layer, "orange")
- layer.set_line_width(2)
-
-
- ui.image(game, layer,
- xtar + game.current["camera"][0],
- ytar + game.current["camera"][1],
- "assets/elements/characters/shadow.png",
- "following",
- offset=True, dynamic=True)
-
-
- ori = ""
-
-
- if 65362 in game.current["keys"]:
- game.current["state"]["objects"][asset]["xyz"][1] -= 4*0.57142857142857142857*withFPS
- ori = "Up"
-
- if 65364 in game.current["keys"]:
- game.current["state"]["objects"][asset]["xyz"][1] += 4*0.57142857142857142857*withFPS
- ori = "Down"
-
- if 65361 in game.current["keys"]:
- game.current["state"]["objects"][asset]["xyz"][0] -= 4 *withFPS
- if ori:
- ori = ori + "-Left"
- else:
- ori = "Left"
-
-
- if 65363 in game.current["keys"]:
- game.current["state"]["objects"][asset]["xyz"][0] += 4 *withFPS
- if ori:
- ori = ori + "-Right"
- else:
- ori = "Right"
- if ori and ori not in rotations["normal"]:
- ori = "Down"
-
-
- if 32 in game.current["keys"]:
- if game.current["state"]["objects"][asset]["grounded"]:
-
-
- game.current["state"]["objects"][asset]["velocity"][2] = 0.05
-
- if 102 in game.current["keys"]:
- if "following" in game.current["state"]["objects"][asset]:
- del game.current["state"]["objects"][asset]["following"]
- else:
- follow_closest(game, asset)
- game.current["keys"].remove(102)
-
-
- if ori:
- game.current["state"]["objects"][asset]["target-orientation"] = ori
-
-
-
-
- elif "following" in game.current["state"]["objects"][asset]:
- target_asset = game.current["state"]["objects"][asset]["following"]
- xtar = 0
- ytar = 0
- ztar = 0
- try:
- xtar = game.current["state"]["objects"][target_asset]["xyz"][0]
- ytar = game.current["state"]["objects"][target_asset]["xyz"][1]
- ztar = game.current["state"]["objects"][target_asset]["xyz"][2]
- except:
- pass
-
- xob = game.current["state"]["objects"][asset]["xyz"][0]
- yob = game.current["state"]["objects"][asset]["xyz"][1]
- zob = game.current["state"]["objects"][asset]["xyz"][2]
-
- distance = ui.distance([xob, yob], [xtar, ytar])
-
- if distance > 50:
- motions = ["Right",
- "Down",
- "Left",
- "Up",
- "Up-Right",
- "Down-Right",
- "Down-Left",
- "Up-Left"]
- possibles = [[xob+4*withFPS,yob, zob],
- [xob, yob+4*0.57142857142857142857*withFPS, zob],
- [xob-4*withFPS, yob, zob],
- [xob, yob-4*0.57142857142857142857*withFPS, zob],
-
- [xob+4*withFPS, yob-4*0.57142857142857142857*withFPS, zob],
- [xob+4*withFPS, yob+4*0.57142857142857142857*withFPS, zob],
- [xob-4*withFPS, yob+4*0.57142857142857142857*withFPS, zob],
- [xob-4*withFPS, yob-4*0.57142857142857142857*withFPS, zob]]
- closest = 0
-
- for n, i in enumerate(possibles):
- thistance = ui.distance([i[0],i[1]], [xtar, ytar])
- if thistance < distance:
- distance = thistance
- closest = n
- game.current["state"]["objects"][asset]["xyz"] = possibles[closest]
- game.current["state"]["objects"][asset]["target-orientation"] = motions[closest]
- if ztar > zob+0.5 and game.current["state"]["objects"][asset]["grounded"]:
- game.current["state"]["objects"][asset]["velocity"][2] = 0.05
-
-
- if game.current["testing"] and "following" in game.current["state"]["objects"][asset]:
- xob = game.current["state"]["objects"][asset]["xyz"][0]
- yob = game.current["state"]["objects"][asset]["xyz"][1]
- zob = game.current["state"]["objects"][asset]["xyz"][2]
- target_asset = game.current["state"]["objects"][asset]["following"]
- xtar = 0
- ytar = 0
- ztar = 0
- try:
- xtar = game.current["state"]["objects"][target_asset]["xyz"][0]
- ytar = game.current["state"]["objects"][target_asset]["xyz"][1]
- ztar = game.current["state"]["objects"][target_asset]["xyz"][2]
- except:
- pass
- camx = game.current["camera"][0]
- camy = game.current["camera"][1]
- ui.color(game, layer, "orange")
- layer.set_line_width(2)
- layer.move_to(xob+camx, yob+camy)
- di = ui.distance([xob, yob], [xtar, ytar])
-
- xlar = xob + ((xtar-xob) / di * (di-20))
- ylar = yob + ((ytar-yob) / di * (di-20))
- layer.line_to(xlar+camx, ylar+camy)
- layer.stroke()
- layer.arc(xtar+camx, ytar+camy, 20, 0, math.pi)
- layer.stroke()
-
-
-
- if game.current["state"]["objects"][asset]["grounded"]:
- game.current["state"]["objects"][asset]["time-falling"] = 0
- if game.current["frame"] % 20 == 0:
- game.current["state"]["objects"][asset]["check_mark"] = game.current["state"]["objects"][asset]["xyz"].copy()
- else:
- game.current["state"]["objects"][asset]["time-falling"] += 1
- if game.current["state"]["objects"][asset]["time-falling"] > 100:
- game.current["state"]["objects"][asset]["time-falling"] = 0
- game.current["state"]["objects"][asset]["xyz"] = game.current["state"]["objects"][asset]["check_mark"].copy()
- game.current["state"]["objects"][asset]["xyz"][2] += 1
- game.current["state"]["objects"][asset]["xyz"][1] -= 34
- game.current["state"]["objects"][asset]["velocity"] = [0,0,0]
-
-
-
- if 65289 in game.current["keys"]:
-
- found = 0
- for i, t in enumerate(game.current["state"]["controlling"]):
- if game.current["state"]["controlling"][t]:
- game.current["state"]["controlling"][t] = False
- found = i
- found += 1
- found = found % len(game.current["state"]["controlling"])
- found = list(game.current["state"]["controlling"].keys())[found]
- game.current["state"]["controlling"][found] = True
- game.current["keys"].remove(65289)
-
-
-
- try:
- targetx = 0 - x79 + (game.current["w"] / 2)
- targety = 0 - y79 + (game.current["h"] / 2)
- game.current["camera"][2] = int(round(z79))
-
- game.current["camera"][0] += (targetx - game.current["camera"][0])/4
- game.current["camera"][1] += (targety - game.current["camera"][1])/4
- except:
- pass
-
- return surface
- def follow_closest(game, asset):
- follow = 0
- dis = 1000000000
- xthis = game.current["state"]["objects"][asset]["xyz"][0]
- ythis = game.current["state"]["objects"][asset]["xyz"][1]
-
- for n, target_asset in enumerate(game.current["state"]["controlling"]):
- if target_asset == asset:
- continue
-
- xtar = game.current["state"]["objects"][target_asset]["xyz"][0]
- ytar = game.current["state"]["objects"][target_asset]["xyz"][1]
- di = ui.distance([xtar,ytar],[xthis,ythis])
- if di < dis:
- dis = di
- follow = target_asset
-
-
- game.current["state"]["objects"][asset]["following"] = follow
-
- def follow_last(game, asset):
- last = list(game.current["state"]["controlling"].keys())[-1]
- game.current["state"]["objects"][asset]["following"] = last
|