<commit_before>﻿// Copyright 2017 Google Inc. All rights reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//     http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

using UnityEngine;
using System;
using System.Collections.Generic;

namespace Hamster {
  // Monobehaviour for the actual unity gameobject that represents the map.
  // Largely concerned with spawning the scene representation of a level
  // and tearing it down when done.
  class GameWorld : MonoBehaviour {
    // Dictioary of scene object representations of the map tiles and elements.
    // The key is the same key as they have in the database table.  (A unique
    // string based on their position and type.)
    Dictionary<string, GameObject> sceneObjects = new Dictionary<string, GameObject>();
    public LevelMap worldMap = new LevelMap();

    // Iterates through a map and spawns all the objects in it.
    public void SpawnWorld(LevelMap map) {
      foreach (MapElement element in map.elements.Values) {
        GameObject obj = PlaceTile(element);
        obj.transform.localScale = element.scale;
      }
      worldMap.SetProperties(map.name, map.mapId, map.ownerId);
    }

    public void DisposeWorld() {
      worldMap.elements.Clear();
      foreach (GameObject obj in sceneObjects.Values) {
        Destroy(obj);
      }
      sceneObjects.Clear();
      worldMap.ResetProperties();
    }

    // Internal utility function for removing an item from the map, based on its
    // dictionary key.  (The dictionary key is the string returned from
    // MapElement::GetKey())
    void RemoveObject(string key) {
      worldMap.elements.Remove(key);
      Destroy(sceneObjects[key]);
      sceneObjects.Remove(key);
    }

    // Spawns a single object in the world, based on a map element.
    public GameObject PlaceTile(MapElement element) {
      string key = element.GetStringKey();

      // If we're placing a unique object, or we're placing an object over an existing object,
      // then remove the old one that is being replaced.
      if (worldMap.elements.ContainsKey(key)) {
        RemoveObject(key);
      }

      // Add the new object.  (Or add nothing, if we're "adding" an empty blocks)

      GameObject obj = SpawnElement(element);
      if (obj != null) {
        worldMap.elements.Add(key, element);
        sceneObjects.Add(element.GetStringKey(), obj);
      }
      return obj;
    }

    GameObject SpawnElement(MapElement element) {
      GameObject obj = null;
      PrefabList.PrefabEntry elementDef;
      if (CommonData.prefabs.lookup.TryGetValue(element.type, out elementDef)) {
        if (elementDef.prefab != null) {
          obj = (GameObject)Instantiate(elementDef.prefab, element.position, element.rotation);
        }
      } else {
        throw new Exception(
            "SpawnElement: type did not match any registered prefabs: [" + element.type + "]");
      }
      return obj;<commit_msg>Lowering the game world to be on the Z plane.<commit_after>﻿// Copyright 2017 Google Inc. All rights reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//     http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

using UnityEngine;
using System;
using System.Collections.Generic;

namespace Hamster {
  // Monobehaviour for the actual unity gameobject that represents the map.
  // Largely concerned with spawning the scene representation of a level
  // and tearing it down when done.
  class GameWorld : MonoBehaviour {
    // Dictioary of scene object representations of the map tiles and elements.
    // The key is the same key as they have in the database table.  (A unique
    // string based on their position and type.)
    Dictionary<string, GameObject> sceneObjects = new Dictionary<string, GameObject>();
    public LevelMap worldMap = new LevelMap();

    // When spawning in elements, adjust their positions by this amount.
    Vector3 elementAdjust = new Vector3(0.0f, -0.5f, 0.0f);

    // Iterates through a map and spawns all the objects in it.
    public void SpawnWorld(LevelMap map) {
      foreach (MapElement element in map.elements.Values) {
        GameObject obj = PlaceTile(element);
        obj.transform.localScale = element.scale;
      }
      worldMap.SetProperties(map.name, map.mapId, map.ownerId);
    }

    public void DisposeWorld() {
      worldMap.elements.Clear();
      foreach (GameObject obj in sceneObjects.Values) {
        Destroy(obj);
      }
      sceneObjects.Clear();
      worldMap.ResetProperties();
    }

    // Internal utility function for removing an item from the map, based on its
    // dictionary key.  (The dictionary key is the string returned from
    // MapElement::GetKey())
    void RemoveObject(string key) {
      worldMap.elements.Remove(key);
      Destroy(sceneObjects[key]);
      sceneObjects.Remove(key);
    }

    // Spawns a single object in the world, based on a map element.
    public GameObject PlaceTile(MapElement element) {
      string key = element.GetStringKey();

      // If we're placing a unique object, or we're placing an object over an existing object,
      // then remove the old one that is being replaced.
      if (worldMap.elements.ContainsKey(key)) {
        RemoveObject(key);
      }

      // Add the new object.  (Or add nothing, if we're "adding" an empty blocks)

      GameObject obj = SpawnElement(element);
      if (obj != null) {
        worldMap.elements.Add(key, element);
        sceneObjects.Add(element.GetStringKey(), obj);
      }
      return obj;
    }

    GameObject SpawnElement(MapElement element) {
      GameObject obj = null;
      PrefabList.PrefabEntry elementDef;
      if (CommonData.prefabs.lookup.TryGetValue(element.type, out elementDef)) {
        if (elementDef.prefab != null) {
          obj = (GameObject)Instantiate(elementDef.prefab, element.position + elementAdjust,
                                        element.rotation);
        }
      } else {
        throw new Exception(
            "SpawnElement: type did not match any registered prefabs: [" + element.type + "]");
      }
      return obj;
{'fraction_non_alphanumeric': 0.08960778700257659, 'fraction_numerical': 0.0034354423131978244, 'mean_word_length': 2.791101465002713, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 1, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4022163', 'n_tokens_mistral': 1858, 'n_tokens_neox': 1816, 'n_words': 709}